acpiphp_glue.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * ACPI PCI HotPlug glue functions to ACPI CA subsystem
  4. *
  5. * Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
  6. * Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
  7. * Copyright (C) 2002,2003 NEC Corporation
  8. * Copyright (C) 2003-2005 Matthew Wilcox (willy@infradead.org)
  9. * Copyright (C) 2003-2005 Hewlett Packard
  10. * Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
  11. * Copyright (C) 2005 Intel Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * Send feedback to <kristen.c.accardi@intel.com>
  16. *
  17. */
  18. /*
  19. * Lifetime rules for pci_dev:
  20. * - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
  21. * when the bridge is scanned and it loses a refcount when the bridge
  22. * is removed.
  23. * - When a P2P bridge is present, we elevate the refcount on the subordinate
  24. * bus. It loses the refcount when the the driver unloads.
  25. */
  26. #define pr_fmt(fmt) "acpiphp_glue: " fmt
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/pci.h>
  30. #include <linux/pci_hotplug.h>
  31. #include <linux/pci-acpi.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/mutex.h>
  34. #include <linux/slab.h>
  35. #include <linux/acpi.h>
  36. #include "../pci.h"
  37. #include "acpiphp.h"
  38. static LIST_HEAD(bridge_list);
  39. static DEFINE_MUTEX(bridge_mutex);
  40. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type);
  41. static void acpiphp_post_dock_fixup(struct acpi_device *adev);
  42. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  43. static void hotplug_event(u32 type, struct acpiphp_context *context);
  44. static void free_bridge(struct kref *kref);
  45. /**
  46. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  47. * @adev: ACPI device object to create the context for.
  48. *
  49. * Call under acpi_hp_context_lock.
  50. */
  51. static struct acpiphp_context *acpiphp_init_context(struct acpi_device *adev)
  52. {
  53. struct acpiphp_context *context;
  54. context = kzalloc(sizeof(*context), GFP_KERNEL);
  55. if (!context)
  56. return NULL;
  57. context->refcount = 1;
  58. context->hp.notify = acpiphp_hotplug_notify;
  59. context->hp.fixup = acpiphp_post_dock_fixup;
  60. acpi_set_hp_context(adev, &context->hp);
  61. return context;
  62. }
  63. /**
  64. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  65. * @adev: ACPI device object to get the context for.
  66. *
  67. * Call under acpi_hp_context_lock.
  68. */
  69. static struct acpiphp_context *acpiphp_get_context(struct acpi_device *adev)
  70. {
  71. struct acpiphp_context *context;
  72. if (!adev->hp)
  73. return NULL;
  74. context = to_acpiphp_context(adev->hp);
  75. context->refcount++;
  76. return context;
  77. }
  78. /**
  79. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  80. * @context: ACPI hotplug context to drop a reference to.
  81. *
  82. * The context object is removed if there are no more references to it.
  83. *
  84. * Call under acpi_hp_context_lock.
  85. */
  86. static void acpiphp_put_context(struct acpiphp_context *context)
  87. {
  88. if (--context->refcount)
  89. return;
  90. WARN_ON(context->bridge);
  91. context->hp.self->hp = NULL;
  92. kfree(context);
  93. }
  94. static inline void get_bridge(struct acpiphp_bridge *bridge)
  95. {
  96. kref_get(&bridge->ref);
  97. }
  98. static inline void put_bridge(struct acpiphp_bridge *bridge)
  99. {
  100. kref_put(&bridge->ref, free_bridge);
  101. }
  102. static struct acpiphp_context *acpiphp_grab_context(struct acpi_device *adev)
  103. {
  104. struct acpiphp_context *context;
  105. acpi_lock_hp_context();
  106. context = acpiphp_get_context(adev);
  107. if (!context || context->func.parent->is_going_away) {
  108. acpi_unlock_hp_context();
  109. return NULL;
  110. }
  111. get_bridge(context->func.parent);
  112. acpiphp_put_context(context);
  113. acpi_unlock_hp_context();
  114. return context;
  115. }
  116. static void acpiphp_let_context_go(struct acpiphp_context *context)
  117. {
  118. put_bridge(context->func.parent);
  119. }
  120. static void free_bridge(struct kref *kref)
  121. {
  122. struct acpiphp_context *context;
  123. struct acpiphp_bridge *bridge;
  124. struct acpiphp_slot *slot, *next;
  125. struct acpiphp_func *func, *tmp;
  126. acpi_lock_hp_context();
  127. bridge = container_of(kref, struct acpiphp_bridge, ref);
  128. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  129. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  130. acpiphp_put_context(func_to_context(func));
  131. kfree(slot);
  132. }
  133. context = bridge->context;
  134. /* Root bridges will not have hotplug context. */
  135. if (context) {
  136. /* Release the reference taken by acpiphp_enumerate_slots(). */
  137. put_bridge(context->func.parent);
  138. context->bridge = NULL;
  139. acpiphp_put_context(context);
  140. }
  141. put_device(&bridge->pci_bus->dev);
  142. pci_dev_put(bridge->pci_dev);
  143. kfree(bridge);
  144. acpi_unlock_hp_context();
  145. }
  146. /**
  147. * acpiphp_post_dock_fixup - Post-dock fixups for PCI devices.
  148. * @adev: ACPI device object corresponding to a PCI device.
  149. *
  150. * TBD - figure out a way to only call fixups for systems that require them.
  151. */
  152. static void acpiphp_post_dock_fixup(struct acpi_device *adev)
  153. {
  154. struct acpiphp_context *context = acpiphp_grab_context(adev);
  155. struct pci_bus *bus;
  156. u32 buses;
  157. if (!context)
  158. return;
  159. bus = context->func.slot->bus;
  160. if (!bus->self)
  161. goto out;
  162. /* fixup bad _DCK function that rewrites
  163. * secondary bridge on slot
  164. */
  165. pci_read_config_dword(bus->self, PCI_PRIMARY_BUS, &buses);
  166. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  167. buses = (buses & 0xff000000)
  168. | ((unsigned int)(bus->primary) << 0)
  169. | ((unsigned int)(bus->busn_res.start) << 8)
  170. | ((unsigned int)(bus->busn_res.end) << 16);
  171. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  172. }
  173. out:
  174. acpiphp_let_context_go(context);
  175. }
  176. /**
  177. * acpiphp_add_context - Add ACPIPHP context to an ACPI device object.
  178. * @handle: ACPI handle of the object to add a context to.
  179. * @lvl: Not used.
  180. * @data: The object's parent ACPIPHP bridge.
  181. * @rv: Not used.
  182. */
  183. static acpi_status acpiphp_add_context(acpi_handle handle, u32 lvl, void *data,
  184. void **rv)
  185. {
  186. struct acpiphp_bridge *bridge = data;
  187. struct acpiphp_context *context;
  188. struct acpi_device *adev;
  189. struct acpiphp_slot *slot;
  190. struct acpiphp_func *newfunc;
  191. acpi_status status = AE_OK;
  192. unsigned long long adr;
  193. int device, function;
  194. struct pci_bus *pbus = bridge->pci_bus;
  195. struct pci_dev *pdev = bridge->pci_dev;
  196. u32 val;
  197. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  198. if (ACPI_FAILURE(status)) {
  199. if (status != AE_NOT_FOUND)
  200. acpi_handle_warn(handle,
  201. "can't evaluate _ADR (%#x)\n", status);
  202. return AE_OK;
  203. }
  204. if (acpi_bus_get_device(handle, &adev))
  205. return AE_OK;
  206. device = (adr >> 16) & 0xffff;
  207. function = adr & 0xffff;
  208. acpi_lock_hp_context();
  209. context = acpiphp_init_context(adev);
  210. if (!context) {
  211. acpi_unlock_hp_context();
  212. acpi_handle_err(handle, "No hotplug context\n");
  213. return AE_NOT_EXIST;
  214. }
  215. newfunc = &context->func;
  216. newfunc->function = function;
  217. newfunc->parent = bridge;
  218. acpi_unlock_hp_context();
  219. /*
  220. * If this is a dock device, its _EJ0 should be executed by the dock
  221. * notify handler after calling _DCK.
  222. */
  223. if (!is_dock_device(adev) && acpi_has_method(handle, "_EJ0"))
  224. newfunc->flags = FUNC_HAS_EJ0;
  225. if (acpi_has_method(handle, "_STA"))
  226. newfunc->flags |= FUNC_HAS_STA;
  227. /* search for objects that share the same slot */
  228. list_for_each_entry(slot, &bridge->slots, node)
  229. if (slot->device == device)
  230. goto slot_found;
  231. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  232. if (!slot) {
  233. acpi_lock_hp_context();
  234. acpiphp_put_context(context);
  235. acpi_unlock_hp_context();
  236. return AE_NO_MEMORY;
  237. }
  238. slot->bus = bridge->pci_bus;
  239. slot->device = device;
  240. INIT_LIST_HEAD(&slot->funcs);
  241. list_add_tail(&slot->node, &bridge->slots);
  242. /*
  243. * Expose slots to user space for functions that have _EJ0 or _RMV or
  244. * are located in dock stations. Do not expose them for devices handled
  245. * by the native PCIe hotplug (PCIeHP) or standard PCI hotplug
  246. * (SHPCHP), because that code is supposed to expose slots to user
  247. * space in those cases.
  248. */
  249. if ((acpi_pci_check_ejectable(pbus, handle) || is_dock_device(adev))
  250. && !(pdev && hotplug_is_native(pdev))) {
  251. unsigned long long sun;
  252. int retval;
  253. bridge->nr_slots++;
  254. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  255. if (ACPI_FAILURE(status))
  256. sun = bridge->nr_slots;
  257. pr_debug("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  258. sun, pci_domain_nr(pbus), pbus->number, device);
  259. retval = acpiphp_register_hotplug_slot(slot, sun);
  260. if (retval) {
  261. slot->slot = NULL;
  262. bridge->nr_slots--;
  263. if (retval == -EBUSY)
  264. pr_warn("Slot %llu already registered by another hotplug driver\n", sun);
  265. else
  266. pr_warn("acpiphp_register_hotplug_slot failed (err code = 0x%x)\n", retval);
  267. }
  268. /* Even if the slot registration fails, we can still use it. */
  269. }
  270. slot_found:
  271. newfunc->slot = slot;
  272. list_add_tail(&newfunc->sibling, &slot->funcs);
  273. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  274. &val, 60*1000))
  275. slot->flags |= SLOT_ENABLED;
  276. return AE_OK;
  277. }
  278. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  279. {
  280. struct acpiphp_slot *slot;
  281. struct acpiphp_func *func;
  282. list_for_each_entry(slot, &bridge->slots, node) {
  283. list_for_each_entry(func, &slot->funcs, sibling) {
  284. struct acpi_device *adev = func_to_acpi_device(func);
  285. acpi_lock_hp_context();
  286. adev->hp->notify = NULL;
  287. adev->hp->fixup = NULL;
  288. acpi_unlock_hp_context();
  289. }
  290. slot->flags |= SLOT_IS_GOING_AWAY;
  291. if (slot->slot)
  292. acpiphp_unregister_hotplug_slot(slot);
  293. }
  294. mutex_lock(&bridge_mutex);
  295. list_del(&bridge->list);
  296. mutex_unlock(&bridge_mutex);
  297. acpi_lock_hp_context();
  298. bridge->is_going_away = true;
  299. acpi_unlock_hp_context();
  300. }
  301. /**
  302. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  303. * @bus: bus to start search with
  304. */
  305. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  306. {
  307. struct pci_bus *tmp;
  308. unsigned char max, n;
  309. /*
  310. * pci_bus_max_busnr will return the highest
  311. * reserved busnr for all these children.
  312. * that is equivalent to the bus->subordinate
  313. * value. We don't want to use the parent's
  314. * bus->subordinate value because it could have
  315. * padding in it.
  316. */
  317. max = bus->busn_res.start;
  318. list_for_each_entry(tmp, &bus->children, node) {
  319. n = pci_bus_max_busnr(tmp);
  320. if (n > max)
  321. max = n;
  322. }
  323. return max;
  324. }
  325. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  326. {
  327. struct acpiphp_func *func;
  328. union acpi_object params[2];
  329. struct acpi_object_list arg_list;
  330. list_for_each_entry(func, &slot->funcs, sibling) {
  331. arg_list.count = 2;
  332. arg_list.pointer = params;
  333. params[0].type = ACPI_TYPE_INTEGER;
  334. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  335. params[1].type = ACPI_TYPE_INTEGER;
  336. params[1].integer.value = 1;
  337. /* _REG is optional, we don't care about if there is failure */
  338. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  339. NULL);
  340. }
  341. }
  342. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  343. {
  344. struct acpiphp_func *func;
  345. /* quirk, or pcie could set it already */
  346. if (dev->is_hotplug_bridge)
  347. return;
  348. list_for_each_entry(func, &slot->funcs, sibling) {
  349. if (PCI_FUNC(dev->devfn) == func->function) {
  350. dev->is_hotplug_bridge = 1;
  351. break;
  352. }
  353. }
  354. }
  355. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  356. {
  357. struct acpiphp_func *func;
  358. list_for_each_entry(func, &slot->funcs, sibling) {
  359. struct acpi_device *adev = func_to_acpi_device(func);
  360. acpi_bus_scan(adev->handle);
  361. if (acpi_device_enumerated(adev))
  362. acpi_device_set_power(adev, ACPI_STATE_D0);
  363. }
  364. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  365. }
  366. static void acpiphp_native_scan_bridge(struct pci_dev *bridge)
  367. {
  368. struct pci_bus *bus = bridge->subordinate;
  369. struct pci_dev *dev;
  370. int max;
  371. if (!bus)
  372. return;
  373. max = bus->busn_res.start;
  374. /* Scan already configured non-hotplug bridges */
  375. for_each_pci_bridge(dev, bus) {
  376. if (!hotplug_is_native(dev))
  377. max = pci_scan_bridge(bus, dev, max, 0);
  378. }
  379. /* Scan non-hotplug bridges that need to be reconfigured */
  380. for_each_pci_bridge(dev, bus) {
  381. if (!hotplug_is_native(dev))
  382. max = pci_scan_bridge(bus, dev, max, 1);
  383. }
  384. }
  385. /**
  386. * enable_slot - enable, configure a slot
  387. * @slot: slot to be enabled
  388. * @bridge: true if enable is for the whole bridge (not a single slot)
  389. *
  390. * This function should be called per *physical slot*,
  391. * not per each slot object in ACPI namespace.
  392. */
  393. static void enable_slot(struct acpiphp_slot *slot, bool bridge)
  394. {
  395. struct pci_dev *dev;
  396. struct pci_bus *bus = slot->bus;
  397. struct acpiphp_func *func;
  398. if (bridge && bus->self && hotplug_is_native(bus->self)) {
  399. /*
  400. * If native hotplug is used, it will take care of hotplug
  401. * slot management and resource allocation for hotplug
  402. * bridges. However, ACPI hotplug may still be used for
  403. * non-hotplug bridges to bring in additional devices such
  404. * as a Thunderbolt host controller.
  405. */
  406. for_each_pci_bridge(dev, bus) {
  407. if (PCI_SLOT(dev->devfn) == slot->device)
  408. acpiphp_native_scan_bridge(dev);
  409. }
  410. pci_assign_unassigned_bridge_resources(bus->self);
  411. } else {
  412. LIST_HEAD(add_list);
  413. int max, pass;
  414. acpiphp_rescan_slot(slot);
  415. max = acpiphp_max_busnr(bus);
  416. for (pass = 0; pass < 2; pass++) {
  417. for_each_pci_bridge(dev, bus) {
  418. if (PCI_SLOT(dev->devfn) != slot->device)
  419. continue;
  420. max = pci_scan_bridge(bus, dev, max, pass);
  421. if (pass && dev->subordinate) {
  422. check_hotplug_bridge(slot, dev);
  423. pcibios_resource_survey_bus(dev->subordinate);
  424. __pci_bus_size_bridges(dev->subordinate,
  425. &add_list);
  426. }
  427. }
  428. }
  429. __pci_bus_assign_resources(bus, &add_list, NULL);
  430. }
  431. acpiphp_sanitize_bus(bus);
  432. pcie_bus_configure_settings(bus);
  433. acpiphp_set_acpi_region(slot);
  434. list_for_each_entry(dev, &bus->devices, bus_list) {
  435. /* Assume that newly added devices are powered on already. */
  436. if (!pci_dev_is_added(dev))
  437. dev->current_state = PCI_D0;
  438. }
  439. pci_bus_add_devices(bus);
  440. slot->flags |= SLOT_ENABLED;
  441. list_for_each_entry(func, &slot->funcs, sibling) {
  442. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  443. func->function));
  444. if (!dev) {
  445. /* Do not set SLOT_ENABLED flag if some funcs
  446. are not added. */
  447. slot->flags &= ~SLOT_ENABLED;
  448. continue;
  449. }
  450. }
  451. }
  452. /**
  453. * disable_slot - disable a slot
  454. * @slot: ACPI PHP slot
  455. */
  456. static void disable_slot(struct acpiphp_slot *slot)
  457. {
  458. struct pci_bus *bus = slot->bus;
  459. struct pci_dev *dev, *prev;
  460. struct acpiphp_func *func;
  461. /*
  462. * enable_slot() enumerates all functions in this device via
  463. * pci_scan_slot(), whether they have associated ACPI hotplug
  464. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  465. * here.
  466. */
  467. list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
  468. if (PCI_SLOT(dev->devfn) == slot->device)
  469. pci_stop_and_remove_bus_device(dev);
  470. list_for_each_entry(func, &slot->funcs, sibling)
  471. acpi_bus_trim(func_to_acpi_device(func));
  472. slot->flags &= ~SLOT_ENABLED;
  473. }
  474. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  475. {
  476. struct pci_bus *bus = slot->bus;
  477. struct pci_dev *dev;
  478. list_for_each_entry(dev, &bus->devices, bus_list) {
  479. if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
  480. return true;
  481. }
  482. return false;
  483. }
  484. /**
  485. * get_slot_status - get ACPI slot status
  486. * @slot: ACPI PHP slot
  487. *
  488. * If a slot has _STA for each function and if any one of them
  489. * returned non-zero status, return it.
  490. *
  491. * If a slot doesn't have _STA and if any one of its functions'
  492. * configuration space is configured, return 0x0f as a _STA.
  493. *
  494. * Otherwise return 0.
  495. */
  496. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  497. {
  498. unsigned long long sta = 0;
  499. struct acpiphp_func *func;
  500. u32 dvid;
  501. list_for_each_entry(func, &slot->funcs, sibling) {
  502. if (func->flags & FUNC_HAS_STA) {
  503. acpi_status status;
  504. status = acpi_evaluate_integer(func_to_handle(func),
  505. "_STA", NULL, &sta);
  506. if (ACPI_SUCCESS(status) && sta)
  507. break;
  508. } else {
  509. if (pci_bus_read_dev_vendor_id(slot->bus,
  510. PCI_DEVFN(slot->device, func->function),
  511. &dvid, 0)) {
  512. sta = ACPI_STA_ALL;
  513. break;
  514. }
  515. }
  516. }
  517. if (!sta) {
  518. /*
  519. * Check for the slot itself since it may be that the
  520. * ACPI slot is a device below PCIe upstream port so in
  521. * that case it may not even be reachable yet.
  522. */
  523. if (pci_bus_read_dev_vendor_id(slot->bus,
  524. PCI_DEVFN(slot->device, 0), &dvid, 0)) {
  525. sta = ACPI_STA_ALL;
  526. }
  527. }
  528. return (unsigned int)sta;
  529. }
  530. static inline bool device_status_valid(unsigned int sta)
  531. {
  532. /*
  533. * ACPI spec says that _STA may return bit 0 clear with bit 3 set
  534. * if the device is valid but does not require a device driver to be
  535. * loaded (Section 6.3.7 of ACPI 5.0A).
  536. */
  537. unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
  538. return (sta & mask) == mask;
  539. }
  540. /**
  541. * trim_stale_devices - remove PCI devices that are not responding.
  542. * @dev: PCI device to start walking the hierarchy from.
  543. */
  544. static void trim_stale_devices(struct pci_dev *dev)
  545. {
  546. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  547. struct pci_bus *bus = dev->subordinate;
  548. bool alive = dev->ignore_hotplug;
  549. if (adev) {
  550. acpi_status status;
  551. unsigned long long sta;
  552. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  553. alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
  554. }
  555. if (!alive)
  556. alive = pci_device_is_present(dev);
  557. if (!alive) {
  558. pci_dev_set_disconnected(dev, NULL);
  559. if (pci_has_subordinate(dev))
  560. pci_walk_bus(dev->subordinate, pci_dev_set_disconnected,
  561. NULL);
  562. pci_stop_and_remove_bus_device(dev);
  563. if (adev)
  564. acpi_bus_trim(adev);
  565. } else if (bus) {
  566. struct pci_dev *child, *tmp;
  567. /* The device is a bridge. so check the bus below it. */
  568. pm_runtime_get_sync(&dev->dev);
  569. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  570. trim_stale_devices(child);
  571. pm_runtime_put(&dev->dev);
  572. }
  573. }
  574. /**
  575. * acpiphp_check_bridge - re-enumerate devices
  576. * @bridge: where to begin re-enumeration
  577. *
  578. * Iterate over all slots under this bridge and make sure that if a
  579. * card is present they are enabled, and if not they are disabled.
  580. */
  581. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  582. {
  583. struct acpiphp_slot *slot;
  584. /* Bail out if the bridge is going away. */
  585. if (bridge->is_going_away)
  586. return;
  587. if (bridge->pci_dev)
  588. pm_runtime_get_sync(&bridge->pci_dev->dev);
  589. list_for_each_entry(slot, &bridge->slots, node) {
  590. struct pci_bus *bus = slot->bus;
  591. struct pci_dev *dev, *tmp;
  592. if (slot_no_hotplug(slot)) {
  593. ; /* do nothing */
  594. } else if (device_status_valid(get_slot_status(slot))) {
  595. /* remove stale devices if any */
  596. list_for_each_entry_safe_reverse(dev, tmp,
  597. &bus->devices, bus_list)
  598. if (PCI_SLOT(dev->devfn) == slot->device)
  599. trim_stale_devices(dev);
  600. /* configure all functions */
  601. enable_slot(slot, true);
  602. } else {
  603. disable_slot(slot);
  604. }
  605. }
  606. if (bridge->pci_dev)
  607. pm_runtime_put(&bridge->pci_dev->dev);
  608. }
  609. /*
  610. * Remove devices for which we could not assign resources, call
  611. * arch specific code to fix-up the bus
  612. */
  613. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  614. {
  615. struct pci_dev *dev, *tmp;
  616. int i;
  617. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  618. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  619. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  620. struct resource *res = &dev->resource[i];
  621. if ((res->flags & type_mask) && !res->start &&
  622. res->end) {
  623. /* Could not assign a required resources
  624. * for this device, remove it */
  625. pci_stop_and_remove_bus_device(dev);
  626. break;
  627. }
  628. }
  629. }
  630. }
  631. /*
  632. * ACPI event handlers
  633. */
  634. void acpiphp_check_host_bridge(struct acpi_device *adev)
  635. {
  636. struct acpiphp_bridge *bridge = NULL;
  637. acpi_lock_hp_context();
  638. if (adev->hp) {
  639. bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
  640. if (bridge)
  641. get_bridge(bridge);
  642. }
  643. acpi_unlock_hp_context();
  644. if (bridge) {
  645. pci_lock_rescan_remove();
  646. acpiphp_check_bridge(bridge);
  647. pci_unlock_rescan_remove();
  648. put_bridge(bridge);
  649. }
  650. }
  651. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  652. static void hotplug_event(u32 type, struct acpiphp_context *context)
  653. {
  654. acpi_handle handle = context->hp.self->handle;
  655. struct acpiphp_func *func = &context->func;
  656. struct acpiphp_slot *slot = func->slot;
  657. struct acpiphp_bridge *bridge;
  658. acpi_lock_hp_context();
  659. bridge = context->bridge;
  660. if (bridge)
  661. get_bridge(bridge);
  662. acpi_unlock_hp_context();
  663. pci_lock_rescan_remove();
  664. switch (type) {
  665. case ACPI_NOTIFY_BUS_CHECK:
  666. /* bus re-enumerate */
  667. acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
  668. if (bridge)
  669. acpiphp_check_bridge(bridge);
  670. else if (!(slot->flags & SLOT_IS_GOING_AWAY))
  671. enable_slot(slot, false);
  672. break;
  673. case ACPI_NOTIFY_DEVICE_CHECK:
  674. /* device check */
  675. acpi_handle_debug(handle, "Device check in %s()\n", __func__);
  676. if (bridge) {
  677. acpiphp_check_bridge(bridge);
  678. } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
  679. /*
  680. * Check if anything has changed in the slot and rescan
  681. * from the parent if that's the case.
  682. */
  683. if (acpiphp_rescan_slot(slot))
  684. acpiphp_check_bridge(func->parent);
  685. }
  686. break;
  687. case ACPI_NOTIFY_EJECT_REQUEST:
  688. /* request device eject */
  689. acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
  690. acpiphp_disable_and_eject_slot(slot);
  691. break;
  692. }
  693. pci_unlock_rescan_remove();
  694. if (bridge)
  695. put_bridge(bridge);
  696. }
  697. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
  698. {
  699. struct acpiphp_context *context;
  700. context = acpiphp_grab_context(adev);
  701. if (!context)
  702. return -ENODATA;
  703. hotplug_event(type, context);
  704. acpiphp_let_context_go(context);
  705. return 0;
  706. }
  707. /**
  708. * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
  709. * @bus: PCI bus to enumerate the slots for.
  710. *
  711. * A "slot" is an object associated with a PCI device number. All functions
  712. * (PCI devices) with the same bus and device number belong to the same slot.
  713. */
  714. void acpiphp_enumerate_slots(struct pci_bus *bus)
  715. {
  716. struct acpiphp_bridge *bridge;
  717. struct acpi_device *adev;
  718. acpi_handle handle;
  719. acpi_status status;
  720. if (acpiphp_disabled)
  721. return;
  722. adev = ACPI_COMPANION(bus->bridge);
  723. if (!adev)
  724. return;
  725. handle = adev->handle;
  726. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  727. if (!bridge)
  728. return;
  729. INIT_LIST_HEAD(&bridge->slots);
  730. kref_init(&bridge->ref);
  731. bridge->pci_dev = pci_dev_get(bus->self);
  732. bridge->pci_bus = bus;
  733. /*
  734. * Grab a ref to the subordinate PCI bus in case the bus is
  735. * removed via PCI core logical hotplug. The ref pins the bus
  736. * (which we access during module unload).
  737. */
  738. get_device(&bus->dev);
  739. acpi_lock_hp_context();
  740. if (pci_is_root_bus(bridge->pci_bus)) {
  741. struct acpiphp_root_context *root_context;
  742. root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
  743. if (!root_context)
  744. goto err;
  745. root_context->root_bridge = bridge;
  746. acpi_set_hp_context(adev, &root_context->hp);
  747. } else {
  748. struct acpiphp_context *context;
  749. /*
  750. * This bridge should have been registered as a hotplug function
  751. * under its parent, so the context should be there, unless the
  752. * parent is going to be handled by pciehp, in which case this
  753. * bridge is not interesting to us either.
  754. */
  755. context = acpiphp_get_context(adev);
  756. if (!context)
  757. goto err;
  758. bridge->context = context;
  759. context->bridge = bridge;
  760. /* Get a reference to the parent bridge. */
  761. get_bridge(context->func.parent);
  762. }
  763. acpi_unlock_hp_context();
  764. /* Must be added to the list prior to calling acpiphp_add_context(). */
  765. mutex_lock(&bridge_mutex);
  766. list_add(&bridge->list, &bridge_list);
  767. mutex_unlock(&bridge_mutex);
  768. /* register all slot objects under this bridge */
  769. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  770. acpiphp_add_context, NULL, bridge, NULL);
  771. if (ACPI_FAILURE(status)) {
  772. acpi_handle_err(handle, "failed to register slots\n");
  773. cleanup_bridge(bridge);
  774. put_bridge(bridge);
  775. }
  776. return;
  777. err:
  778. acpi_unlock_hp_context();
  779. put_device(&bus->dev);
  780. pci_dev_put(bridge->pci_dev);
  781. kfree(bridge);
  782. }
  783. static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
  784. {
  785. if (pci_is_root_bus(bridge->pci_bus)) {
  786. struct acpiphp_root_context *root_context;
  787. struct acpi_device *adev;
  788. acpi_lock_hp_context();
  789. adev = ACPI_COMPANION(bridge->pci_bus->bridge);
  790. root_context = to_acpiphp_root_context(adev->hp);
  791. adev->hp = NULL;
  792. acpi_unlock_hp_context();
  793. kfree(root_context);
  794. }
  795. cleanup_bridge(bridge);
  796. put_bridge(bridge);
  797. }
  798. /**
  799. * acpiphp_remove_slots - Remove slot objects associated with a given bus.
  800. * @bus: PCI bus to remove the slot objects for.
  801. */
  802. void acpiphp_remove_slots(struct pci_bus *bus)
  803. {
  804. struct acpiphp_bridge *bridge;
  805. if (acpiphp_disabled)
  806. return;
  807. mutex_lock(&bridge_mutex);
  808. list_for_each_entry(bridge, &bridge_list, list)
  809. if (bridge->pci_bus == bus) {
  810. mutex_unlock(&bridge_mutex);
  811. acpiphp_drop_bridge(bridge);
  812. return;
  813. }
  814. mutex_unlock(&bridge_mutex);
  815. }
  816. /**
  817. * acpiphp_enable_slot - power on slot
  818. * @slot: ACPI PHP slot
  819. */
  820. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  821. {
  822. pci_lock_rescan_remove();
  823. if (slot->flags & SLOT_IS_GOING_AWAY) {
  824. pci_unlock_rescan_remove();
  825. return -ENODEV;
  826. }
  827. /* configure all functions */
  828. if (!(slot->flags & SLOT_ENABLED))
  829. enable_slot(slot, false);
  830. pci_unlock_rescan_remove();
  831. return 0;
  832. }
  833. /**
  834. * acpiphp_disable_and_eject_slot - power off and eject slot
  835. * @slot: ACPI PHP slot
  836. */
  837. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  838. {
  839. struct acpiphp_func *func;
  840. if (slot->flags & SLOT_IS_GOING_AWAY)
  841. return -ENODEV;
  842. /* unconfigure all functions */
  843. disable_slot(slot);
  844. list_for_each_entry(func, &slot->funcs, sibling)
  845. if (func->flags & FUNC_HAS_EJ0) {
  846. acpi_handle handle = func_to_handle(func);
  847. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  848. acpi_handle_err(handle, "_EJ0 failed\n");
  849. break;
  850. }
  851. return 0;
  852. }
  853. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  854. {
  855. int ret;
  856. /*
  857. * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
  858. * acpiphp_disable_and_eject_slot() will be synchronized properly.
  859. */
  860. acpi_scan_lock_acquire();
  861. pci_lock_rescan_remove();
  862. ret = acpiphp_disable_and_eject_slot(slot);
  863. pci_unlock_rescan_remove();
  864. acpi_scan_lock_release();
  865. return ret;
  866. }
  867. /*
  868. * slot enabled: 1
  869. * slot disabled: 0
  870. */
  871. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  872. {
  873. return (slot->flags & SLOT_ENABLED);
  874. }
  875. /*
  876. * latch open: 1
  877. * latch closed: 0
  878. */
  879. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  880. {
  881. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  882. }
  883. /*
  884. * adapter presence : 1
  885. * absence : 0
  886. */
  887. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  888. {
  889. return !!get_slot_status(slot);
  890. }