20a1a9fecc72fd563e141ceccb53391c44ea69ec
[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/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/spinlock.h>
32 #include <linux/pm.h>
33 #include <linux/pci.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37
38
39 #define _COMPONENT              ACPI_PCI_COMPONENT
40 ACPI_MODULE_NAME                ("pci_root")
41
42 #define ACPI_PCI_ROOT_CLASS             "pci_bridge"
43 #define ACPI_PCI_ROOT_HID               "PNP0A03"
44 #define ACPI_PCI_ROOT_DRIVER_NAME       "ACPI PCI Root Bridge Driver"
45 #define ACPI_PCI_ROOT_DEVICE_NAME       "PCI Root Bridge"
46
47 extern struct pci_ops *pci_root_ops;
48
49 static int acpi_pci_root_add (struct acpi_device *device);
50 static int acpi_pci_root_remove (struct acpi_device *device, int type);
51
52 static struct acpi_driver acpi_pci_root_driver = {
53         .name =         ACPI_PCI_ROOT_DRIVER_NAME,
54         .class =        ACPI_PCI_ROOT_CLASS,
55         .ids =          ACPI_PCI_ROOT_HID,
56         .ops =          {
57                                 .add =    acpi_pci_root_add,
58                                 .remove = acpi_pci_root_remove,
59                         },
60 };
61
62 struct acpi_pci_root {
63         struct list_head        node;
64         acpi_handle             handle;
65         struct acpi_pci_id      id;
66         struct pci_bus          *bus;
67         u64                     mem_tra;
68         u64                     io_tra;
69 };
70
71 struct list_head                acpi_pci_roots;
72
73 static struct acpi_pci_driver *sub_driver;
74
75 int acpi_pci_register_driver(struct acpi_pci_driver *driver)
76 {
77         int n = 0;
78         struct list_head *entry;
79
80         struct acpi_pci_driver **pptr = &sub_driver;
81         while (*pptr)
82                 pptr = &(*pptr)->next;
83         *pptr = driver;
84
85         if (!driver->add)
86                 return 0;
87
88         list_for_each(entry, &acpi_pci_roots) {
89                 struct acpi_pci_root *root;
90                 root = list_entry(entry, struct acpi_pci_root, node);
91                 driver->add(root->handle);
92                 n++;
93         }
94
95         return n;
96 }
97
98 void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
99 {
100         struct list_head *entry;
101
102         struct acpi_pci_driver **pptr = &sub_driver;
103         while (*pptr) {
104                 if (*pptr != driver)
105                         continue;
106                 *pptr = (*pptr)->next;
107                 break;
108         }
109
110         if (!driver->remove)
111                 return;
112
113         list_for_each(entry, &acpi_pci_roots) {
114                 struct acpi_pci_root *root;
115                 root = list_entry(entry, struct acpi_pci_root, node);
116                 driver->remove(root->handle);
117         }
118 }
119
120 void
121 acpi_pci_get_translations (
122         struct acpi_pci_id      *id,
123         u64                     *mem_tra,
124         u64                     *io_tra)
125 {
126         struct list_head        *node = NULL;
127         struct acpi_pci_root    *entry;
128
129         /* TBD: Locking */
130         list_for_each(node, &acpi_pci_roots) {
131                 entry = list_entry(node, struct acpi_pci_root, node);
132                 if ((id->segment == entry->id.segment)
133                         && (id->bus == entry->id.bus)) {
134                         *mem_tra = entry->mem_tra;
135                         *io_tra = entry->io_tra;
136                         return;
137                 }
138         }
139
140         *mem_tra = 0;
141         *io_tra = 0;
142 }
143
144
145 static u64
146 acpi_pci_root_bus_tra (
147        struct acpi_resource     *resource,
148        int                      type)
149 {
150         struct acpi_resource_address16 *address16;
151         struct acpi_resource_address32 *address32;
152         struct acpi_resource_address64 *address64;
153
154         while (1) {
155                 switch (resource->id) {
156                 case ACPI_RSTYPE_END_TAG:
157                         return 0;
158
159                 case ACPI_RSTYPE_ADDRESS16:
160                         address16 = (struct acpi_resource_address16 *) &resource->data;
161                         if (type == address16->resource_type) {
162                                 return address16->address_translation_offset;
163                         }
164                         break;
165
166                 case ACPI_RSTYPE_ADDRESS32:
167                         address32 = (struct acpi_resource_address32 *) &resource->data;
168                         if (type == address32->resource_type) {
169                                 return address32->address_translation_offset;
170                         }
171                         break;
172
173                 case ACPI_RSTYPE_ADDRESS64:
174                         address64 = (struct acpi_resource_address64 *) &resource->data;
175                         if (type == address64->resource_type) {
176                                 return address64->address_translation_offset;
177                         }
178                         break;
179                 }
180                 resource = ACPI_PTR_ADD (struct acpi_resource,
181                                 resource, resource->length);
182         }
183
184         return 0;
185 }
186
187
188 static int
189 acpi_pci_evaluate_crs (
190         struct acpi_pci_root    *root)
191 {
192         acpi_status             status;
193         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
194
195         ACPI_FUNCTION_TRACE("acpi_pci_evaluate_crs");
196
197         status = acpi_get_current_resources (root->handle, &buffer);
198         if (ACPI_FAILURE(status))
199                 return_VALUE(-ENODEV);
200
201         root->io_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
202                         buffer.pointer, ACPI_IO_RANGE);
203         root->mem_tra = acpi_pci_root_bus_tra ((struct acpi_resource *)
204                         buffer.pointer, ACPI_MEMORY_RANGE);
205
206         acpi_os_free(buffer.pointer);
207         return_VALUE(0);
208 }
209
210
211 static int
212 acpi_pci_root_add (
213         struct acpi_device      *device)
214 {
215         int                     result = 0;
216         struct acpi_pci_root    *root = NULL;
217         acpi_status             status = AE_OK;
218         unsigned long           value = 0;
219         acpi_handle             handle = NULL;
220
221         ACPI_FUNCTION_TRACE("acpi_pci_root_add");
222
223         if (!device)
224                 return_VALUE(-EINVAL);
225
226         root = kmalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
227         if (!root)
228                 return_VALUE(-ENOMEM);
229         memset(root, 0, sizeof(struct acpi_pci_root));
230
231         root->handle = device->handle;
232         sprintf(acpi_device_name(device), "%s", ACPI_PCI_ROOT_DEVICE_NAME);
233         sprintf(acpi_device_class(device), "%s", ACPI_PCI_ROOT_CLASS);
234         acpi_driver_data(device) = root;
235
236         /*
237          * TBD: Doesn't the bus driver automatically set this?
238          */
239         device->ops.bind = acpi_pci_bind;
240
241         /* 
242          * Segment
243          * -------
244          * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
245          */
246         status = acpi_evaluate_integer(root->handle, METHOD_NAME__SEG, NULL, 
247                 &value);
248         switch (status) {
249         case AE_OK:
250                 root->id.segment = (u16) value;
251                 printk("_SEG exists! Unsupported. Abort.\n");
252                 BUG();
253                 break;
254         case AE_NOT_FOUND:
255                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
256                         "Assuming segment 0 (no _SEG)\n"));
257                 root->id.segment = 0;
258                 break;
259         default:
260                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _SEG\n"));
261                 result = -ENODEV;
262                 goto end;
263         }
264
265         /* 
266          * Bus
267          * ---
268          * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
269          */
270         status = acpi_evaluate_integer(root->handle, METHOD_NAME__BBN, NULL, 
271                 &value);
272         switch (status) {
273         case AE_OK:
274                 root->id.bus = (u16) value;
275                 break;
276         case AE_NOT_FOUND:
277                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
278                 root->id.bus = 0;
279                 break;
280         default:
281                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BBN\n"));
282                 result = -ENODEV;
283                 goto end;
284         }
285
286         /*
287          * Device & Function
288          * -----------------
289          * Obtained from _ADR (which has already been evaluated for us).
290          */
291         root->id.device = device->pnp.bus_address >> 16;
292         root->id.function = device->pnp.bus_address & 0xFFFF;
293
294         /*
295          * Evaluate _CRS to get root bridge resources
296          * TBD: Need PCI interface for enumeration/configuration of roots.
297          */
298         acpi_pci_evaluate_crs(root);
299
300         /* TBD: Locking */
301         list_add_tail(&root->node, &acpi_pci_roots);
302
303         printk(KERN_INFO PREFIX "%s [%s] (%02x:%02x)\n", 
304                 acpi_device_name(device), acpi_device_bid(device),
305                 root->id.segment, root->id.bus);
306
307         /*
308          * Scan the Root Bridge
309          * --------------------
310          * Must do this prior to any attempt to bind the root device, as the
311          * PCI namespace does not get created until this call is made (and 
312          * thus the root bridge's pci_dev does not exist).
313          */
314         root->bus = pcibios_scan_root(root->id.bus);
315         if (!root->bus) {
316                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
317                         "Bus %02x:%02x not present in PCI namespace\n", 
318                         root->id.segment, root->id.bus));
319                 result = -ENODEV;
320                 goto end;
321         }
322
323         /*
324          * Attach ACPI-PCI Context
325          * -----------------------
326          * Thus binding the ACPI and PCI devices.
327          */
328         result = acpi_pci_bind_root(device, &root->id, root->bus);
329         if (result)
330                 goto end;
331
332         /*
333          * PCI Routing Table
334          * -----------------
335          * Evaluate and parse _PRT, if exists.
336          */
337         status = acpi_get_handle(root->handle, METHOD_NAME__PRT, &handle);
338         if (ACPI_SUCCESS(status))
339                 result = acpi_pci_irq_add_prt(root->handle, root->id.segment,
340                         root->id.bus);
341
342 end:
343         if (result)
344                 kfree(root);
345
346         return_VALUE(result);
347 }
348
349
350 static int
351 acpi_pci_root_remove (
352         struct acpi_device      *device,
353         int                     type)
354 {
355         struct acpi_pci_root    *root = NULL;
356
357         ACPI_FUNCTION_TRACE("acpi_pci_root_remove");
358
359         if (!device || !acpi_driver_data(device))
360                 return_VALUE(-EINVAL);
361
362         root = (struct acpi_pci_root *) acpi_driver_data(device);
363
364         kfree(root);
365
366         return_VALUE(0);
367 }
368
369
370 static int __init acpi_pci_root_init (void)
371 {
372         ACPI_FUNCTION_TRACE("acpi_pci_root_init");
373
374         if (acpi_disabled)
375                 return_VALUE(0);
376
377         /* DEBUG:
378         acpi_dbg_layer = ACPI_PCI_COMPONENT;
379         acpi_dbg_level = 0xFFFFFFFF;
380          */
381
382         INIT_LIST_HEAD(&acpi_pci_roots);
383
384         if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
385                 return_VALUE(-ENODEV);
386
387         return_VALUE(0);
388 }
389
390 subsys_initcall(acpi_pci_root_init);
391