acpiphp_glue.c 26 KB

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