pci_root.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. /**
  122. * acpi_is_root_bridge - determine whether an ACPI CA node is a PCI root bridge
  123. * @handle - the ACPI CA node in question.
  124. *
  125. * Note: we could make this API take a struct acpi_device * instead, but
  126. * for now, it's more convenient to operate on an acpi_handle.
  127. */
  128. int acpi_is_root_bridge(acpi_handle handle)
  129. {
  130. int ret;
  131. struct acpi_device *device;
  132. ret = acpi_bus_get_device(handle, &device);
  133. if (ret)
  134. return 0;
  135. ret = acpi_match_device_ids(device, root_device_ids);
  136. if (ret)
  137. return 0;
  138. else
  139. return 1;
  140. }
  141. EXPORT_SYMBOL_GPL(acpi_is_root_bridge);
  142. static acpi_status
  143. get_root_bridge_busnr_callback(struct acpi_resource *resource, void *data)
  144. {
  145. int *busnr = data;
  146. struct acpi_resource_address64 address;
  147. if (resource->type != ACPI_RESOURCE_TYPE_ADDRESS16 &&
  148. resource->type != ACPI_RESOURCE_TYPE_ADDRESS32 &&
  149. resource->type != ACPI_RESOURCE_TYPE_ADDRESS64)
  150. return AE_OK;
  151. acpi_resource_to_address64(resource, &address);
  152. if ((address.address_length > 0) &&
  153. (address.resource_type == ACPI_BUS_NUMBER_RANGE))
  154. *busnr = address.minimum;
  155. return AE_OK;
  156. }
  157. static acpi_status try_get_root_bridge_busnr(acpi_handle handle, int *busnum)
  158. {
  159. acpi_status status;
  160. *busnum = -1;
  161. status =
  162. acpi_walk_resources(handle, METHOD_NAME__CRS,
  163. get_root_bridge_busnr_callback, busnum);
  164. if (ACPI_FAILURE(status))
  165. return status;
  166. /* Check if we really get a bus number from _CRS */
  167. if (*busnum == -1)
  168. return AE_ERROR;
  169. return AE_OK;
  170. }
  171. static void acpi_pci_bridge_scan(struct acpi_device *device)
  172. {
  173. int status;
  174. struct acpi_device *child = NULL;
  175. if (device->flags.bus_address)
  176. if (device->parent && device->parent->ops.bind) {
  177. status = device->parent->ops.bind(device);
  178. if (!status) {
  179. list_for_each_entry(child, &device->children, node)
  180. acpi_pci_bridge_scan(child);
  181. }
  182. }
  183. }
  184. static u8 OSC_UUID[16] = {0x5B, 0x4D, 0xDB, 0x33, 0xF7, 0x1F, 0x1C, 0x40,
  185. 0x96, 0x57, 0x74, 0x41, 0xC0, 0x3D, 0xD7, 0x66};
  186. static acpi_status acpi_pci_run_osc(acpi_handle handle,
  187. const u32 *capbuf, u32 *retval)
  188. {
  189. acpi_status status;
  190. struct acpi_object_list input;
  191. union acpi_object in_params[4];
  192. struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
  193. union acpi_object *out_obj;
  194. u32 errors;
  195. /* Setting up input parameters */
  196. input.count = 4;
  197. input.pointer = in_params;
  198. in_params[0].type = ACPI_TYPE_BUFFER;
  199. in_params[0].buffer.length = 16;
  200. in_params[0].buffer.pointer = OSC_UUID;
  201. in_params[1].type = ACPI_TYPE_INTEGER;
  202. in_params[1].integer.value = 1;
  203. in_params[2].type = ACPI_TYPE_INTEGER;
  204. in_params[2].integer.value = 3;
  205. in_params[3].type = ACPI_TYPE_BUFFER;
  206. in_params[3].buffer.length = 12;
  207. in_params[3].buffer.pointer = (u8 *)capbuf;
  208. status = acpi_evaluate_object(handle, "_OSC", &input, &output);
  209. if (ACPI_FAILURE(status))
  210. return status;
  211. if (!output.length)
  212. return AE_NULL_OBJECT;
  213. out_obj = output.pointer;
  214. if (out_obj->type != ACPI_TYPE_BUFFER) {
  215. printk(KERN_DEBUG "_OSC evaluation returned wrong type\n");
  216. status = AE_TYPE;
  217. goto out_kfree;
  218. }
  219. /* Need to ignore the bit0 in result code */
  220. errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
  221. if (errors) {
  222. if (errors & OSC_REQUEST_ERROR)
  223. printk(KERN_DEBUG "_OSC request failed\n");
  224. if (errors & OSC_INVALID_UUID_ERROR)
  225. printk(KERN_DEBUG "_OSC invalid UUID\n");
  226. if (errors & OSC_INVALID_REVISION_ERROR)
  227. printk(KERN_DEBUG "_OSC invalid revision\n");
  228. if (errors & OSC_CAPABILITIES_MASK_ERROR) {
  229. if (capbuf[OSC_QUERY_TYPE] & OSC_QUERY_ENABLE)
  230. goto out_success;
  231. printk(KERN_DEBUG
  232. "Firmware did not grant requested _OSC control\n");
  233. status = AE_SUPPORT;
  234. goto out_kfree;
  235. }
  236. status = AE_ERROR;
  237. goto out_kfree;
  238. }
  239. out_success:
  240. *retval = *((u32 *)(out_obj->buffer.pointer + 8));
  241. status = AE_OK;
  242. out_kfree:
  243. kfree(output.pointer);
  244. return status;
  245. }
  246. static acpi_status acpi_pci_query_osc(struct acpi_pci_root *root, u32 flags)
  247. {
  248. acpi_status status;
  249. u32 support_set, result, capbuf[3];
  250. /* do _OSC query for all possible controls */
  251. support_set = root->osc_support_set | (flags & OSC_SUPPORT_MASKS);
  252. capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
  253. capbuf[OSC_SUPPORT_TYPE] = support_set;
  254. capbuf[OSC_CONTROL_TYPE] = OSC_CONTROL_MASKS;
  255. status = acpi_pci_run_osc(root->device->handle, capbuf, &result);
  256. if (ACPI_SUCCESS(status)) {
  257. root->osc_support_set = support_set;
  258. root->osc_control_qry = result;
  259. root->osc_queried = 1;
  260. }
  261. return status;
  262. }
  263. static acpi_status acpi_pci_osc_support(struct acpi_pci_root *root, u32 flags)
  264. {
  265. acpi_status status;
  266. acpi_handle tmp;
  267. status = acpi_get_handle(root->device->handle, "_OSC", &tmp);
  268. if (ACPI_FAILURE(status))
  269. return status;
  270. mutex_lock(&osc_lock);
  271. status = acpi_pci_query_osc(root, flags);
  272. mutex_unlock(&osc_lock);
  273. return status;
  274. }
  275. static struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle)
  276. {
  277. struct acpi_pci_root *root;
  278. list_for_each_entry(root, &acpi_pci_roots, node) {
  279. if (root->device->handle == handle)
  280. return root;
  281. }
  282. return NULL;
  283. }
  284. /**
  285. * acpi_pci_osc_control_set - commit requested control to Firmware
  286. * @handle: acpi_handle for the target ACPI object
  287. * @flags: driver's requested control bits
  288. *
  289. * Attempt to take control from Firmware on requested control bits.
  290. **/
  291. acpi_status acpi_pci_osc_control_set(acpi_handle handle, u32 flags)
  292. {
  293. acpi_status status;
  294. u32 control_req, result, capbuf[3];
  295. acpi_handle tmp;
  296. struct acpi_pci_root *root;
  297. status = acpi_get_handle(handle, "_OSC", &tmp);
  298. if (ACPI_FAILURE(status))
  299. return status;
  300. control_req = (flags & OSC_CONTROL_MASKS);
  301. if (!control_req)
  302. return AE_TYPE;
  303. root = acpi_pci_find_root(handle);
  304. if (!root)
  305. return AE_NOT_EXIST;
  306. mutex_lock(&osc_lock);
  307. /* No need to evaluate _OSC if the control was already granted. */
  308. if ((root->osc_control_set & control_req) == control_req)
  309. goto out;
  310. /* Need to query controls first before requesting them */
  311. if (!root->osc_queried) {
  312. status = acpi_pci_query_osc(root, root->osc_support_set);
  313. if (ACPI_FAILURE(status))
  314. goto out;
  315. }
  316. if ((root->osc_control_qry & control_req) != control_req) {
  317. printk(KERN_DEBUG
  318. "Firmware did not grant requested _OSC control\n");
  319. status = AE_SUPPORT;
  320. goto out;
  321. }
  322. capbuf[OSC_QUERY_TYPE] = 0;
  323. capbuf[OSC_SUPPORT_TYPE] = root->osc_support_set;
  324. capbuf[OSC_CONTROL_TYPE] = root->osc_control_set | control_req;
  325. status = acpi_pci_run_osc(handle, capbuf, &result);
  326. if (ACPI_SUCCESS(status))
  327. root->osc_control_set = result;
  328. out:
  329. mutex_unlock(&osc_lock);
  330. return status;
  331. }
  332. EXPORT_SYMBOL(acpi_pci_osc_control_set);
  333. static int __devinit acpi_pci_root_add(struct acpi_device *device)
  334. {
  335. int result = 0;
  336. struct acpi_pci_root *root = NULL;
  337. struct acpi_pci_root *tmp;
  338. acpi_status status = AE_OK;
  339. unsigned long long value = 0;
  340. acpi_handle handle = NULL;
  341. struct acpi_device *child;
  342. u32 flags, base_flags;
  343. if (!device)
  344. return -EINVAL;
  345. root = kzalloc(sizeof(struct acpi_pci_root), GFP_KERNEL);
  346. if (!root)
  347. return -ENOMEM;
  348. INIT_LIST_HEAD(&root->node);
  349. root->device = device;
  350. strcpy(acpi_device_name(device), ACPI_PCI_ROOT_DEVICE_NAME);
  351. strcpy(acpi_device_class(device), ACPI_PCI_ROOT_CLASS);
  352. device->driver_data = root;
  353. /*
  354. * All supported architectures that use ACPI have support for
  355. * PCI domains, so we indicate this in _OSC support capabilities.
  356. */
  357. flags = base_flags = OSC_PCI_SEGMENT_GROUPS_SUPPORT;
  358. acpi_pci_osc_support(root, flags);
  359. /*
  360. * Segment
  361. * -------
  362. * Obtained via _SEG, if exists, otherwise assumed to be zero (0).
  363. */
  364. status = acpi_evaluate_integer(device->handle, METHOD_NAME__SEG, NULL,
  365. &value);
  366. switch (status) {
  367. case AE_OK:
  368. root->id.segment = (u16) value;
  369. break;
  370. case AE_NOT_FOUND:
  371. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  372. "Assuming segment 0 (no _SEG)\n"));
  373. root->id.segment = 0;
  374. break;
  375. default:
  376. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _SEG"));
  377. result = -ENODEV;
  378. goto end;
  379. }
  380. /*
  381. * Bus
  382. * ---
  383. * Obtained via _BBN, if exists, otherwise assumed to be zero (0).
  384. */
  385. status = acpi_evaluate_integer(device->handle, METHOD_NAME__BBN, NULL,
  386. &value);
  387. switch (status) {
  388. case AE_OK:
  389. root->id.bus = (u16) value;
  390. break;
  391. case AE_NOT_FOUND:
  392. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Assuming bus 0 (no _BBN)\n"));
  393. root->id.bus = 0;
  394. break;
  395. default:
  396. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BBN"));
  397. result = -ENODEV;
  398. goto end;
  399. }
  400. /* Some systems have wrong _BBN */
  401. list_for_each_entry(tmp, &acpi_pci_roots, node) {
  402. if ((tmp->id.segment == root->id.segment)
  403. && (tmp->id.bus == root->id.bus)) {
  404. int bus = 0;
  405. acpi_status status;
  406. printk(KERN_ERR PREFIX
  407. "Wrong _BBN value, reboot"
  408. " and use option 'pci=noacpi'\n");
  409. status = try_get_root_bridge_busnr(device->handle, &bus);
  410. if (ACPI_FAILURE(status))
  411. break;
  412. if (bus != root->id.bus) {
  413. printk(KERN_INFO PREFIX
  414. "PCI _CRS %d overrides _BBN 0\n", bus);
  415. root->id.bus = bus;
  416. }
  417. break;
  418. }
  419. }
  420. /*
  421. * Device & Function
  422. * -----------------
  423. * Obtained from _ADR (which has already been evaluated for us).
  424. */
  425. root->id.device = device->pnp.bus_address >> 16;
  426. root->id.function = device->pnp.bus_address & 0xFFFF;
  427. /*
  428. * TBD: Need PCI interface for enumeration/configuration of roots.
  429. */
  430. /* TBD: Locking */
  431. list_add_tail(&root->node, &acpi_pci_roots);
  432. printk(KERN_INFO PREFIX "%s [%s] (%04x:%02x)\n",
  433. acpi_device_name(device), acpi_device_bid(device),
  434. root->id.segment, root->id.bus);
  435. /*
  436. * Scan the Root Bridge
  437. * --------------------
  438. * Must do this prior to any attempt to bind the root device, as the
  439. * PCI namespace does not get created until this call is made (and
  440. * thus the root bridge's pci_dev does not exist).
  441. */
  442. root->bus = pci_acpi_scan_root(device, root->id.segment, root->id.bus);
  443. if (!root->bus) {
  444. printk(KERN_ERR PREFIX
  445. "Bus %04x:%02x not present in PCI namespace\n",
  446. root->id.segment, root->id.bus);
  447. result = -ENODEV;
  448. goto end;
  449. }
  450. /*
  451. * Attach ACPI-PCI Context
  452. * -----------------------
  453. * Thus binding the ACPI and PCI devices.
  454. */
  455. result = acpi_pci_bind_root(device, &root->id, root->bus);
  456. if (result)
  457. goto end;
  458. /*
  459. * PCI Routing Table
  460. * -----------------
  461. * Evaluate and parse _PRT, if exists.
  462. */
  463. status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
  464. if (ACPI_SUCCESS(status))
  465. result = acpi_pci_irq_add_prt(device->handle, root->id.segment,
  466. root->id.bus);
  467. /*
  468. * Scan and bind all _ADR-Based Devices
  469. */
  470. list_for_each_entry(child, &device->children, node)
  471. acpi_pci_bridge_scan(child);
  472. /* Indicate support for various _OSC capabilities. */
  473. if (pci_ext_cfg_avail(root->bus->self))
  474. flags |= OSC_EXT_PCI_CONFIG_SUPPORT;
  475. if (pcie_aspm_enabled())
  476. flags |= OSC_ACTIVE_STATE_PWR_SUPPORT |
  477. OSC_CLOCK_PWR_CAPABILITY_SUPPORT;
  478. if (pci_msi_enabled())
  479. flags |= OSC_MSI_SUPPORT;
  480. if (flags != base_flags)
  481. acpi_pci_osc_support(root, flags);
  482. end:
  483. if (result) {
  484. if (!list_empty(&root->node))
  485. list_del(&root->node);
  486. kfree(root);
  487. }
  488. return result;
  489. }
  490. static int acpi_pci_root_start(struct acpi_device *device)
  491. {
  492. struct acpi_pci_root *root;
  493. list_for_each_entry(root, &acpi_pci_roots, node) {
  494. if (root->device == device) {
  495. pci_bus_add_devices(root->bus);
  496. return 0;
  497. }
  498. }
  499. return -ENODEV;
  500. }
  501. static int acpi_pci_root_remove(struct acpi_device *device, int type)
  502. {
  503. struct acpi_pci_root *root = NULL;
  504. if (!device || !acpi_driver_data(device))
  505. return -EINVAL;
  506. root = acpi_driver_data(device);
  507. kfree(root);
  508. return 0;
  509. }
  510. static int __init acpi_pci_root_init(void)
  511. {
  512. if (acpi_pci_disabled)
  513. return 0;
  514. if (acpi_bus_register_driver(&acpi_pci_root_driver) < 0)
  515. return -ENODEV;
  516. return 0;
  517. }
  518. subsys_initcall(acpi_pci_root_init);