Update ia64 patch to 2.5.69-030521, throwing away the parts included
[linux-flexiantxendom0-3.2.10.git] / drivers / acpi / pci_root.c
1 /*
2  *  pci_root.c - ACPI PCI Root Bridge Driver ($Revision: 40 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/config.h>
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/proc_fs.h>
33 #include <linux/spinlock.h>
34 #include <linux/pm.h>
35 #include <linux/pci.h>
36 #include <linux/acpi.h>
37 #include <acpi/acpi_bus.h>
38 #include <acpi/acpi_drivers.h>
39
40
41 #define _COMPONENT              ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME                ("pci_root")
43
44 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
45 #define ACPI_PCI_ROOT_HID               "PNP0A03"
46 #define ACPI_PCI_ROOT_DRIVER_NAME       "ACPI PCI Root Bridge Driver"
47 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
48
49 extern struct pci_ops *pci_root_ops;
50
51 static int acpi_pci_root_add (struct acpi_device *device);
52 static int acpi_pci_root_remove (struct acpi_device *device, int type);
53
54 static struct acpi_driver acpi_pci_root_driver = {
55         .name =         ACPI_PCI_ROOT_DRIVER_NAME,
56         .class =        ACPI_PCI_ROOT_CLASS,
57         .ids =          ACPI_PCI_ROOT_HID,
58         .ops =          {
59                                 .add =    acpi_pci_root_add,
60                                 .remove = acpi_pci_root_remove,
61                         },
62 };
63
64  struct acpi_pci_root {
65         struct list_head        node;
66         acpi_handle             handle;
67         struct acpi_pci_id      id;
68         struct pci_bus          *bus;
69         u64                     mem_tra;
70         u64                     io_tra;
71  };
72
73 struct list_head                acpi_pci_roots;
74
75
76 void
77 acpi_pci_get_translations (
78         struct acpi_pci_id      *id,
79         u64                     *mem_tra,
80         u64                     *io_tra)
81 {
82         struct list_head        *node = NULL;
83         struct acpi_pci_root    *entry;
84
85         /* TBD: Locking */
86         list_for_each(node, &acpi_pci_roots) {
87                 entry = list_entry(node, struct acpi_pci_root, node);
88                 if ((id->segment == entry->id.segment)
89                         && (id->bus == entry->id.bus)) {
90                         *mem_tra = entry->mem_tra;
91                         *io_tra = entry->io_tra;
92                         return;
93                 }
94         }
95
96         *mem_tra = 0;
97         *io_tra = 0;
98 }
99
100
101 static u64
102 acpi_pci_root_bus_tra (
103        struct acpi_resource     *resource,
104        int                      type)
105 {
106         struct acpi_resource_address16 *address16;
107         struct acpi_resource_address32 *address32;
108         struct acpi_resource_address64 *address64;
109
110         while (1) {
111                 switch (resource->id) {
112                 case ACPI_RSTYPE_END_TAG:
113                         return 0;
114
115                 case ACPI_RSTYPE_ADDRESS16:
116                         address16 = (struct acpi_resource_address16 *) &resource->data;
117                         if (type == address16->resource_type) {
118                                 return address16->address_translation_offset;
119                         }
120                         break;
121
122                 case ACPI_RSTYPE_ADDRESS32:
123                         address32 = (struct acpi_resource_address32 *) &resource->data;
124                         if (type == address32->resource_type) {
125                                 return address32->address_translation_offset;
126                         }
127                         break;
128
129                 case ACPI_RSTYPE_ADDRESS64:
130                         address64 = (struct acpi_resource_address64 *) &resource->data;
131                         if (type == address64->resource_type) {
132                                 return address64->address_translation_offset;
133                         }
134                         break;
135                 }
136                 resource = ACPI_PTR_ADD (struct acpi_resource,
137                                 resource, resource->length);
138         }
139
140         return 0;
141 }
142
143
144 static int
145 acpi_pci_evaluate_crs (
146         struct acpi_pci_root    *root)
147 {
148         acpi_status             status;
149         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
150
151         ACPI_FUNCTION_TRACE("acpi_pci_evaluate_crs");
152
153         status = acpi_get_current_resources (root->handle, &buffer);
154         if (ACPI_FAILURE(status))
155                 return_VALUE(-ENODEV);
156
157         root->io_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
158                         buffer.pointer, ACPI_IO_RANGE);
159         root->mem_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
160                         buffer.pointer, ACPI_MEMORY_RANGE);
161
162         acpi_os_free(buffer.pointer);
163         return_VALUE(0);
164 }
165
166
167 static int
168 acpi_pci_root_add (
169         struct acpi_device      *device)
170 {
171         int                     result = 0;
172         struct acpi_pci_root    *root = NULL;
173         acpi_status             status = AE_OK;
174         unsigned long           value = 0;
175         acpi_handle             handle = NULL;
176
177         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
178
179         if (!device)
180                 return_VALUE(-EINVAL);
181
182         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
183         if (!root)
184                 return_VALUE(-ENOMEM);
185         memset(root, 0, sizeof(struct acpi_pci_root));
186
187         root->handle = device->handle;
188         sprintf(acpi_device_name(device), "%s", ACPI_PCI_ROOT_DEVICE_NAME);
189         sprintf(acpi_device_class(device), "%s", ACPI_PCI_ROOT_CLASS);
190         acpi_driver_data(device) = root;
191
192         /*
193          * TBD: Doesn't the bus driver automatically set this?
194          */
195         device->ops.bind = acpi_pci_bind;
196
197         /* 
198          * Segment
199          * -------
200          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
201          */
202         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
203                 &value);
204         switch (status) {
205         case AE_OK:
206                 root->id.segment = (u16) value;
207                 break;
208         case AE_NOT_FOUND:
209                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
210                         "Assuming segment 0 (no _SEG)\n"));
211                 root->id.segment = 0;
212                 break;
213         default:
214                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
215                 result = -ENODEV;
216                 goto end;
217         }
218
219         /* 
220          * Bus
221          * ---
222          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
223          */
224         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
225                 &value);
226         switch (status) {
227         case AE_OK:
228                 root->id.bus = (u16) value;
229                 break;
230         case AE_NOT_FOUND:
231                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
232                 root->id.bus = 0;
233                 break;
234         default:
235                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
236                 result = -ENODEV;
237                 goto end;
238         }
239
240         /*
241          * Device & Function
242          * -----------------
243          * Obtained from _ADR (which has already been evaluated for us).
244          */
245         root->id.device = device->pnp.bus_address >> 16;
246         root->id.function = device->pnp.bus_address & 0xFFFF;
247
248         /*
249          * Evaluate _CRS to get root bridge resources
250          * TBD: Need PCI interface for enumeration/configuration of roots.
251          */
252         acpi_pci_evaluate_crs(root);
253
254         /* TBD: Locking */
255         list_add_tail(&root->node, &acpi_pci_roots);
256
257         printk(KERN_INFO PREFIX "%s [%s] (%02x:%02x)\n", 
258                 acpi_device_name(device), acpi_device_bid(device),
259                 root->id.segment, root->id.bus);
260
261         /*
262          * Scan the Root Bridge
263          * --------------------
264          * Must do this prior to any attempt to bind the root device, as the
265          * PCI namespace does not get created until this call is made (and 
266          * thus the root bridge's pci_dev does not exist).
267          */
268 #ifdef CONFIG_X86
269         root->bus = pcibios_scan_root(root->id.bus);
270 #else
271         root->bus = pcibios_scan_root(root->handle,
272                                       root->id.segment, root->id.bus);
273 #endif
274         if (!root->bus) {
275                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
276                         "Bus %02x:%02x not present in PCI namespace\n", 
277                         root->id.segment, root->id.bus));
278                 result = -ENODEV;
279                 goto end;
280         }
281
282         /*
283          * Attach ACPI-PCI Context
284          * -----------------------
285          * Thus binding the ACPI and PCI devices.
286          */
287         result = acpi_pci_bind_root(device, &root->id, root->bus);
288         if (result)
289                 goto end;
290
291         /*
292          * PCI Routing Table
293          * -----------------
294          * Evaluate and parse _PRT, if exists.
295          */
296         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
297         if (ACPI_SUCCESS(status))
298                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
299                         root->id.bus);
300
301 end:
302         if (result)
303                 kfree(root);
304
305         return_VALUE(result);
306 }
307
308
309 static int
310 acpi_pci_root_remove (
311         struct acpi_device      *device,
312         int                     type)
313 {
314         struct acpi_pci_root    *root = NULL;
315
316         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
317
318         if (!device || !acpi_driver_data(device))
319                 return_VALUE(-EINVAL);
320
321         root = (struct acpi_pci_root *) acpi_driver_data(device);
322
323         kfree(root);
324
325         return_VALUE(0);
326 }
327
328
329 static int __init acpi_pci_root_init (void)
330 {
331         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
332
333         if (acpi_disabled)
334                 return_VALUE(0);
335
336         /* DEBUG:
337         acpi_dbg_layer = ACPI_PCI_COMPONENT;
338         acpi_dbg_level = 0xFFFFFFFF;
339          */
340
341         INIT_LIST_HEAD(&acpi_pci_roots);
342
343         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
344                 return_VALUE(-ENODEV);
345
346         return_VALUE(0);
347 }
348
349 subsys_initcall(acpi_pci_root_init);
350