pci_root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/pm.h>
  32. #include <linux/pci.h>
  33. #include <linux/pci-acpi.h>
  34. #include <linux/acpi.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define _COMPONENT ACPI_PCI_COMPONENT
  38. ACPI_MODULE_NAME("pci_root");
  39. #define ACPI_PCI_ROOT_CLASS "pci_bridge"
  40. #define ACPI_PCI_ROOT_DEVICE_NAME "PCI Root Bridge"
  41. static int acpi_pci_root_add(struct acpi_device *device);
  42. static int acpi_pci_root_remove(struct acpi_device *device, int type);
  43. static int acpi_pci_root_start(struct acpi_device *device);
  44. static struct acpi_device_id root_device_ids[] = {
  45. {"PNP0A03", 0},
  46. {"", 0},
  47. };
  48. MODULE_DEVICE_TABLE(acpi, root_device_ids);
  49. static struct acpi_driver acpi_pci_root_driver = {
  50. .name = "pci_root",
  51. .class = ACPI_PCI_ROOT_CLASS,
  52. .ids = root_device_ids,
  53. .ops = {
  54. .add = acpi_pci_root_add,
  55. .remove = acpi_pci_root_remove,
  56. .start = acpi_pci_root_start,
  57. },
  58. };
  59. struct acpi_pci_root {
  60. struct list_head node;
  61. struct acpi_device * device;
  62. struct acpi_pci_id id;
  63. struct pci_bus *bus;
  64. u32 osc_support_set; /* _OSC state of support bits */
  65. u32 osc_control_set; /* _OSC state of control bits */
  66. u32 osc_control_qry; /* the latest _OSC query result */
  67. u32 osc_queried:1; /* has _OSC control been queried? */
  68. };
  69. static LIST_HEAD(acpi_pci_roots);
  70. static struct acpi_pci_driver *sub_driver;
  71. static DEFINE_MUTEX(osc_lock);
  72. int acpi_pci_register_driver(struct acpi_pci_driver *driver)
  73. {
  74. int n = 0;
  75. struct list_head *entry;
  76. struct acpi_pci_driver **pptr = &sub_driver;
  77. while (*pptr)
  78. pptr = &(*pptr)->next;
  79. *pptr = driver;
  80. if (!driver->add)
  81. return 0;
  82. list_for_each(entry, &acpi_pci_roots) {
  83. struct acpi_pci_root *root;
  84. root = list_entry(entry, struct acpi_pci_root, node);
  85. driver->add(root->device->handle);
  86. n++;
  87. }
  88. return n;
  89. }
  90. EXPORT_SYMBOL(acpi_pci_register_driver);
  91. void acpi_pci_unregister_driver(struct acpi_pci_driver *driver)
  92. {
  93. struct list_head *entry;
  94. struct acpi_pci_driver **pptr = &sub_driver;
  95. while (*pptr) {
  96. if (*pptr == driver)
  97. break;
  98. pptr = &(*pptr)->next;
  99. }
  100. BUG_ON(!*pptr);
  101. *pptr = (*pptr)->next;
  102. if (!driver->remove)
  103. return;
  104. list_for_each(entry, &acpi_pci_roots) {
  105. struct acpi_pci_root *root;
  106. root = list_entry(entry, struct acpi_pci_root, node);
  107. driver->remove(root->device->handle);
  108. }
  109. }
  110. EXPORT_SYMBOL(acpi_pci_unregister_driver);
  111. acpi_handle acpi_get_pci_rootbridge_handle(unsigned int seg, unsigned int bus)
  112. {
  113. struct acpi_pci_root *tmp;
  114. list_for_each_entry(tmp, &acpi_pci_roots, node) {
  115. if ((tmp->id.segment == (u16) seg) && (tmp->id.bus == (u16) bus))
  116. return tmp->device->handle;
  117. }
  118. return NULL;
  119. }
  120. EXPORT_SYMBOL_GPL(acpi_get_pci_rootbridge_handle);
  121. static acpi_status
  122. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  123. {
  124. int *busnr = data;
  125. struct acpi_resource_address64 address;
  126. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  127. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  128. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  129. return AE_OK;
  130. acpi_resource_to_address64(resource, &address);
  131. if ((address.address_length > 0) &&
  132. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  133. *busnr = address.minimum;
  134. return AE_OK;
  135. }
  136. static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
  137. {
  138. acpi_status status;
  139. *busnum = -1;
  140. status =
  141. acpi_walk_resources(handle, METHOD_NAME__CRS,
  142. get_root_bridge_busnr_callback, busnum);
  143. if (ACPI_FAILURE(status))
  144. return status;
  145. /* Check if we really get a bus number from _CRS */
  146. if (*busnum == -1)
  147. return AE_ERROR;
  148. return AE_OK;
  149. }
  150. static void acpi_pci_bridge_scan(struct acpi_device *device)
  151. {
  152. int status;
  153. struct acpi_device *child = NULL;
  154. if (device->flags.bus_address)
  155. if (device->parent && device->parent->ops.bind) {
  156. status = device->parent->ops.bind(device);
  157. if (!status) {
  158. list_for_each_entry(child, &device->children, node)
  159. acpi_pci_bridge_scan(child);
  160. }
  161. }
  162. }
  163. static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
  164. 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
  165. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  166. const u32 *capbuf, u32 *retval)
  167. {
  168. acpi_status status;
  169. struct acpi_object_list input;
  170. union acpi_object in_params[4];
  171. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  172. union acpi_object *out_obj;
  173. u32 errors;
  174. /* Setting up input parameters */
  175. input.count = 4;
  176. input.pointer = in_params;
  177. in_params[0].type = ACPI_TYPE_BUFFER;
  178. in_params[0].buffer.length = 16;
  179. in_params[0].buffer.pointer = OSC_UUID;
  180. in_params[1].type = ACPI_TYPE_INTEGER;
  181. in_params[1].integer.value = 1;
  182. in_params[2].type = ACPI_TYPE_INTEGER;
  183. in_params[2].integer.value = 3;
  184. in_params[3].type = ACPI_TYPE_BUFFER;
  185. in_params[3].buffer.length = 12;
  186. in_params[3].buffer.pointer = (u8 *)capbuf;
  187. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  188. if (ACPI_FAILURE(status))
  189. return status;
  190. if (!output.length)
  191. return AE_NULL_OBJECT;
  192. out_obj = output.pointer;
  193. if (out_obj->type != ACPI_TYPE_BUFFER) {
  194. printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
  195. status = AE_TYPE;
  196. goto out_kfree;
  197. }
  198. /* Need to ignore the bit0 in result code */
  199. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  200. if (errors) {
  201. if (errors & OSC_REQUEST_ERROR)
  202. printk(KERN_DEBUG "_OSC request failed\n");
  203. if (errors & OSC_INVALID_UUID_ERROR)
  204. printk(KERN_DEBUG "_OSC invalid UUID\n");
  205. if (errors & OSC_INVALID_REVISION_ERROR)
  206. printk(KERN_DEBUG "_OSC invalid revision\n");
  207. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  208. if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
  209. goto out_success;
  210. printk(KERN_DEBUG
  211. "Firmware did not grant requested _OSC control\n");
  212. status = AE_SUPPORT;
  213. goto out_kfree;
  214. }
  215. status = AE_ERROR;
  216. goto out_kfree;
  217. }
  218. out_success:
  219. *retval = *((u32 *)(out_obj->buffer.pointer + 8));
  220. status = AE_OK;
  221. out_kfree:
  222. kfree(output.pointer);
  223. return status;
  224. }
  225. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
  226. {
  227. acpi_status status;
  228. u32 support_set, result, capbuf[3];
  229. /* do _OSC query for all possible controls */
  230. support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
  231. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  232. capbuf[OSC_SUPPORT_TYPE] = support_set;
  233. capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  234. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  235. if (ACPI_SUCCESS(status)) {
  236. root->osc_support_set = support_set;
  237. root->osc_control_qry = result;
  238. root->osc_queried = 1;
  239. }
  240. return status;
  241. }
  242. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  243. {
  244. acpi_status status;
  245. acpi_handle tmp;
  246. status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
  247. if (ACPI_FAILURE(status))
  248. return status;
  249. mutex_lock(&osc_lock);
  250. status = acpi_pci_query_osc(root, flags);
  251. mutex_unlock(&osc_lock);
  252. return status;
  253. }
  254. static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  255. {
  256. struct acpi_pci_root *root;
  257. list_for_each_entry(root, &acpi_pci_roots, node) {
  258. if (root->device->handle == handle)
  259. return root;
  260. }
  261. return NULL;
  262. }
  263. /**
  264. * acpi_pci_osc_control_set - commit requested control to Firmware
  265. * @handle: acpi_handle for the target ACPI object
  266. * @flags: driver's requested control bits
  267. *
  268. * Attempt to take control from Firmware on requested control bits.
  269. **/
  270. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
  271. {
  272. acpi_status status;
  273. u32 control_req, result, capbuf[3];
  274. acpi_handle tmp;
  275. struct acpi_pci_root *root;
  276. status = acpi_get_handle(handle, "_OSC", &tmp);
  277. if (ACPI_FAILURE(status))
  278. return status;
  279. control_req = (flags & OSC_CONTROL_MASKS);
  280. if (!control_req)
  281. return AE_TYPE;
  282. root = acpi_pci_find_root(handle);
  283. if (!root)
  284. return AE_NOT_EXIST;
  285. mutex_lock(&osc_lock);
  286. /* No need to evaluate _OSC if the control was already granted. */
  287. if ((root->osc_control_set & control_req) == control_req)
  288. goto out;
  289. /* Need to query controls first before requesting them */
  290. if (!root->osc_queried) {
  291. status = acpi_pci_query_osc(root, root->osc_support_set);
  292. if (ACPI_FAILURE(status))
  293. goto out;
  294. }
  295. if ((root->osc_control_qry & control_req) != control_req) {
  296. printk(KERN_DEBUG
  297. "Firmware did not grant requested _OSC control\n");
  298. status = AE_SUPPORT;
  299. goto out;
  300. }
  301. capbuf[OSC_QUERY_TYPE] = 0;
  302. capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
  303. capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
  304. status = acpi_pci_run_osc(handle, capbuf, &result);
  305. if (ACPI_SUCCESS(status))
  306. root->osc_control_set = result;
  307. out:
  308. mutex_unlock(&osc_lock);
  309. return status;
  310. }
  311. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  312. static int __devinit acpi_pci_root_add(struct acpi_device *device)
  313. {
  314. int result = 0;
  315. struct acpi_pci_root *root = NULL;
  316. struct acpi_pci_root *tmp;
  317. acpi_status status = AE_OK;
  318. unsigned long long value = 0;
  319. acpi_handle handle = NULL;
  320. struct acpi_device *child;
  321. u32 flags, base_flags;
  322. if (!device)
  323. return -EINVAL;
  324. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  325. if (!root)
  326. return -ENOMEM;
  327. INIT_LIST_HEAD(&root->node);
  328. root->device = device;
  329. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  330. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  331. device->driver_data = root;
  332. device->ops.bind = acpi_pci_bind;
  333. /*
  334. * All supported architectures that use ACPI have support for
  335. * PCI domains, so we indicate this in _OSC support capabilities.
  336. */
  337. flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  338. acpi_pci_osc_support(root, flags);
  339. /*
  340. * Segment
  341. * -------
  342. * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
  343. */
  344. status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
  345. &value);
  346. switch (status) {
  347. case AE_OK:
  348. root->id.segment = (u16) value;
  349. break;
  350. case AE_NOT_FOUND:
  351. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  352. "Assuming segment 0 (no _SEG)\n"));
  353. root->id.segment = 0;
  354. break;
  355. default:
  356. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
  357. result = -ENODEV;
  358. goto end;
  359. }
  360. /*
  361. * Bus
  362. * ---
  363. * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
  364. */
  365. status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
  366. &value);
  367. switch (status) {
  368. case AE_OK:
  369. root->id.bus = (u16) value;
  370. break;
  371. case AE_NOT_FOUND:
  372. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
  373. root->id.bus = 0;
  374. break;
  375. default:
  376. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
  377. result = -ENODEV;
  378. goto end;
  379. }
  380. /* Some systems have wrong _BBN */
  381. list_for_each_entry(tmp, &acpi_pci_roots, node) {
  382. if ((tmp->id.segment == root->id.segment)
  383. && (tmp->id.bus == root->id.bus)) {
  384. int bus = 0;
  385. acpi_status status;
  386. printk(KERN_ERR PREFIX
  387. "Wrong _BBN value, reboot"
  388. " and use option 'pci=noacpi'\n");
  389. status = try_get_root_bridge_busnr(device->handle, &bus);
  390. if (ACPI_FAILURE(status))
  391. break;
  392. if (bus != root->id.bus) {
  393. printk(KERN_INFO PREFIX
  394. "PCI _CRS %d overrides _BBN 0\n", bus);
  395. root->id.bus = bus;
  396. }
  397. break;
  398. }
  399. }
  400. /*
  401. * Device & Function
  402. * -----------------
  403. * Obtained from _ADR (which has already been evaluated for us).
  404. */
  405. root->id.device = device->pnp.bus_address >> 16;
  406. root->id.function = device->pnp.bus_address & 0xFFFF;
  407. /*
  408. * TBD: Need PCI interface for enumeration/configuration of roots.
  409. */
  410. /* TBD: Locking */
  411. list_add_tail(&root->node, &acpi_pci_roots);
  412. printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
  413. acpi_device_name(device), acpi_device_bid(device),
  414. root->id.segment, root->id.bus);
  415. /*
  416. * Scan the Root Bridge
  417. * --------------------
  418. * Must do this prior to any attempt to bind the root device, as the
  419. * PCI namespace does not get created until this call is made (and
  420. * thus the root bridge's pci_dev does not exist).
  421. */
  422. root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
  423. if (!root->bus) {
  424. printk(KERN_ERR PREFIX
  425. "Bus %04x:%02x not present in PCI namespace\n",
  426. root->id.segment, root->id.bus);
  427. result = -ENODEV;
  428. goto end;
  429. }
  430. /*
  431. * Attach ACPI-PCI Context
  432. * -----------------------
  433. * Thus binding the ACPI and PCI devices.
  434. */
  435. result = acpi_pci_bind_root(device, &root->id, root->bus);
  436. if (result)
  437. goto end;
  438. /*
  439. * PCI Routing Table
  440. * -----------------
  441. * Evaluate and parse _PRT, if exists.
  442. */
  443. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  444. if (ACPI_SUCCESS(status))
  445. result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
  446. root->id.bus);
  447. /*
  448. * Scan and bind all _ADR-Based Devices
  449. */
  450. list_for_each_entry(child, &device->children, node)
  451. acpi_pci_bridge_scan(child);
  452. /* Indicate support for various _OSC capabilities. */
  453. if (pci_ext_cfg_avail(root->bus->self))
  454. flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
  455. if (pcie_aspm_enabled())
  456. flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
  457. OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
  458. if (pci_msi_enabled())
  459. flags |= OSC_MSI_SUPPORT;
  460. if (flags != base_flags)
  461. acpi_pci_osc_support(root, flags);
  462. end:
  463. if (result) {
  464. if (!list_empty(&root->node))
  465. list_del(&root->node);
  466. kfree(root);
  467. }
  468. return result;
  469. }
  470. static int acpi_pci_root_start(struct acpi_device *device)
  471. {
  472. struct acpi_pci_root *root;
  473. list_for_each_entry(root, &acpi_pci_roots, node) {
  474. if (root->device == device) {
  475. pci_bus_add_devices(root->bus);
  476. return 0;
  477. }
  478. }
  479. return -ENODEV;
  480. }
  481. static int acpi_pci_root_remove(struct acpi_device *device, int type)
  482. {
  483. struct acpi_pci_root *root = NULL;
  484. if (!device || !acpi_driver_data(device))
  485. return -EINVAL;
  486. root = acpi_driver_data(device);
  487. kfree(root);
  488. return 0;
  489. }
  490. static int __init acpi_pci_root_init(void)
  491. {
  492. if (acpi_pci_disabled)
  493. return 0;
  494. if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
  495. return -ENODEV;
  496. return 0;
  497. }
  498. subsys_initcall(acpi_pci_root_init);