acpiphp_glue.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 (matthew.wilcox@hp.com)
  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)
  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 acpiphp_bridge *bridge = data;
  191. struct acpiphp_context *context;
  192. struct acpi_device *adev;
  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. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  202. if (ACPI_FAILURE(status)) {
  203. if (status != AE_NOT_FOUND)
  204. acpi_handle_warn(handle,
  205. "can't evaluate _ADR (%#x)\n", status);
  206. return AE_OK;
  207. }
  208. if (acpi_bus_get_device(handle, &adev))
  209. return AE_OK;
  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. union acpi_object params[2];
  333. struct acpi_object_list arg_list;
  334. list_for_each_entry(func, &slot->funcs, sibling) {
  335. arg_list.count = 2;
  336. arg_list.pointer = params;
  337. params[0].type = ACPI_TYPE_INTEGER;
  338. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  339. params[1].type = ACPI_TYPE_INTEGER;
  340. params[1].integer.value = 1;
  341. /* _REG is optional, we don't care about if there is failure */
  342. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  343. NULL);
  344. }
  345. }
  346. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  347. {
  348. struct acpiphp_func *func;
  349. /* quirk, or pcie could set it already */
  350. if (dev->is_hotplug_bridge)
  351. return;
  352. list_for_each_entry(func, &slot->funcs, sibling) {
  353. if (PCI_FUNC(dev->devfn) == func->function) {
  354. dev->is_hotplug_bridge = 1;
  355. break;
  356. }
  357. }
  358. }
  359. static int acpiphp_rescan_slot(struct acpiphp_slot *slot)
  360. {
  361. struct acpiphp_func *func;
  362. list_for_each_entry(func, &slot->funcs, sibling) {
  363. struct acpi_device *adev = func_to_acpi_device(func);
  364. acpi_bus_scan(adev->handle);
  365. if (acpi_device_enumerated(adev))
  366. acpi_device_set_power(adev, ACPI_STATE_D0);
  367. }
  368. return pci_scan_slot(slot->bus, PCI_DEVFN(slot->device, 0));
  369. }
  370. static void acpiphp_native_scan_bridge(struct pci_dev *bridge)
  371. {
  372. struct pci_bus *bus = bridge->subordinate;
  373. struct pci_dev *dev;
  374. int max;
  375. if (!bus)
  376. return;
  377. max = bus->busn_res.start;
  378. /* Scan already configured non-hotplug bridges */
  379. for_each_pci_bridge(dev, bus) {
  380. if (!hotplug_is_native(dev))
  381. max = pci_scan_bridge(bus, dev, max, 0);
  382. }
  383. /* Scan non-hotplug bridges that need to be reconfigured */
  384. for_each_pci_bridge(dev, bus) {
  385. if (hotplug_is_native(dev))
  386. continue;
  387. max = pci_scan_bridge(bus, dev, max, 1);
  388. if (dev->subordinate) {
  389. pcibios_resource_survey_bus(dev->subordinate);
  390. pci_bus_size_bridges(dev->subordinate);
  391. pci_bus_assign_resources(dev->subordinate);
  392. }
  393. }
  394. }
  395. /**
  396. * enable_slot - enable, configure a slot
  397. * @slot: slot to be enabled
  398. * @bridge: true if enable is for the whole bridge (not a single slot)
  399. *
  400. * This function should be called per *physical slot*,
  401. * not per each slot object in ACPI namespace.
  402. */
  403. static void enable_slot(struct acpiphp_slot *slot, bool bridge)
  404. {
  405. struct pci_dev *dev;
  406. struct pci_bus *bus = slot->bus;
  407. struct acpiphp_func *func;
  408. if (bridge && bus->self && hotplug_is_native(bus->self)) {
  409. /*
  410. * If native hotplug is used, it will take care of hotplug
  411. * slot management and resource allocation for hotplug
  412. * bridges. However, ACPI hotplug may still be used for
  413. * non-hotplug bridges to bring in additional devices such
  414. * as a Thunderbolt host controller.
  415. */
  416. for_each_pci_bridge(dev, bus) {
  417. if (PCI_SLOT(dev->devfn) == slot->device)
  418. acpiphp_native_scan_bridge(dev);
  419. }
  420. } else {
  421. LIST_HEAD(add_list);
  422. int max, pass;
  423. acpiphp_rescan_slot(slot);
  424. max = acpiphp_max_busnr(bus);
  425. for (pass = 0; pass < 2; pass++) {
  426. for_each_pci_bridge(dev, bus) {
  427. if (PCI_SLOT(dev->devfn) != slot->device)
  428. continue;
  429. max = pci_scan_bridge(bus, dev, max, pass);
  430. if (pass && dev->subordinate) {
  431. check_hotplug_bridge(slot, dev);
  432. pcibios_resource_survey_bus(dev->subordinate);
  433. __pci_bus_size_bridges(dev->subordinate,
  434. &add_list);
  435. }
  436. }
  437. }
  438. __pci_bus_assign_resources(bus, &add_list, NULL);
  439. }
  440. acpiphp_sanitize_bus(bus);
  441. pcie_bus_configure_settings(bus);
  442. acpiphp_set_acpi_region(slot);
  443. list_for_each_entry(dev, &bus->devices, bus_list) {
  444. /* Assume that newly added devices are powered on already. */
  445. if (!pci_dev_is_added(dev))
  446. dev->current_state = PCI_D0;
  447. }
  448. pci_bus_add_devices(bus);
  449. slot->flags |= SLOT_ENABLED;
  450. list_for_each_entry(func, &slot->funcs, sibling) {
  451. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  452. func->function));
  453. if (!dev) {
  454. /* Do not set SLOT_ENABLED flag if some funcs
  455. are not added. */
  456. slot->flags &= ~SLOT_ENABLED;
  457. continue;
  458. }
  459. pci_dev_put(dev);
  460. }
  461. }
  462. /**
  463. * disable_slot - disable a slot
  464. * @slot: ACPI PHP slot
  465. */
  466. static void disable_slot(struct acpiphp_slot *slot)
  467. {
  468. struct pci_bus *bus = slot->bus;
  469. struct pci_dev *dev, *prev;
  470. struct acpiphp_func *func;
  471. /*
  472. * enable_slot() enumerates all functions in this device via
  473. * pci_scan_slot(), whether they have associated ACPI hotplug
  474. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  475. * here.
  476. */
  477. list_for_each_entry_safe_reverse(dev, prev, &bus->devices, bus_list)
  478. if (PCI_SLOT(dev->devfn) == slot->device)
  479. pci_stop_and_remove_bus_device(dev);
  480. list_for_each_entry(func, &slot->funcs, sibling)
  481. acpi_bus_trim(func_to_acpi_device(func));
  482. slot->flags &= ~SLOT_ENABLED;
  483. }
  484. static bool slot_no_hotplug(struct acpiphp_slot *slot)
  485. {
  486. struct pci_bus *bus = slot->bus;
  487. struct pci_dev *dev;
  488. list_for_each_entry(dev, &bus->devices, bus_list) {
  489. if (PCI_SLOT(dev->devfn) == slot->device && dev->ignore_hotplug)
  490. return true;
  491. }
  492. return false;
  493. }
  494. /**
  495. * get_slot_status - get ACPI slot status
  496. * @slot: ACPI PHP slot
  497. *
  498. * If a slot has _STA for each function and if any one of them
  499. * returned non-zero status, return it.
  500. *
  501. * If a slot doesn't have _STA and if any one of its functions'
  502. * configuration space is configured, return 0x0f as a _STA.
  503. *
  504. * Otherwise return 0.
  505. */
  506. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  507. {
  508. unsigned long long sta = 0;
  509. struct acpiphp_func *func;
  510. u32 dvid;
  511. list_for_each_entry(func, &slot->funcs, sibling) {
  512. if (func->flags & FUNC_HAS_STA) {
  513. acpi_status status;
  514. status = acpi_evaluate_integer(func_to_handle(func),
  515. "_STA", NULL, &sta);
  516. if (ACPI_SUCCESS(status) && sta)
  517. break;
  518. } else {
  519. if (pci_bus_read_dev_vendor_id(slot->bus,
  520. PCI_DEVFN(slot->device, func->function),
  521. &dvid, 0)) {
  522. sta = ACPI_STA_ALL;
  523. break;
  524. }
  525. }
  526. }
  527. if (!sta) {
  528. /*
  529. * Check for the slot itself since it may be that the
  530. * ACPI slot is a device below PCIe upstream port so in
  531. * that case it may not even be reachable yet.
  532. */
  533. if (pci_bus_read_dev_vendor_id(slot->bus,
  534. PCI_DEVFN(slot->device, 0), &dvid, 0)) {
  535. sta = ACPI_STA_ALL;
  536. }
  537. }
  538. return (unsigned int)sta;
  539. }
  540. static inline bool device_status_valid(unsigned int sta)
  541. {
  542. /*
  543. * ACPI spec says that _STA may return bit 0 clear with bit 3 set
  544. * if the device is valid but does not require a device driver to be
  545. * loaded (Section 6.3.7 of ACPI 5.0A).
  546. */
  547. unsigned int mask = ACPI_STA_DEVICE_ENABLED | ACPI_STA_DEVICE_FUNCTIONING;
  548. return (sta & mask) == mask;
  549. }
  550. /**
  551. * trim_stale_devices - remove PCI devices that are not responding.
  552. * @dev: PCI device to start walking the hierarchy from.
  553. */
  554. static void trim_stale_devices(struct pci_dev *dev)
  555. {
  556. struct acpi_device *adev = ACPI_COMPANION(&dev->dev);
  557. struct pci_bus *bus = dev->subordinate;
  558. bool alive = dev->ignore_hotplug;
  559. if (adev) {
  560. acpi_status status;
  561. unsigned long long sta;
  562. status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
  563. alive = alive || (ACPI_SUCCESS(status) && device_status_valid(sta));
  564. }
  565. if (!alive)
  566. alive = pci_device_is_present(dev);
  567. if (!alive) {
  568. pci_dev_set_disconnected(dev, NULL);
  569. if (pci_has_subordinate(dev))
  570. pci_walk_bus(dev->subordinate, pci_dev_set_disconnected,
  571. NULL);
  572. pci_stop_and_remove_bus_device(dev);
  573. if (adev)
  574. acpi_bus_trim(adev);
  575. } else if (bus) {
  576. struct pci_dev *child, *tmp;
  577. /* The device is a bridge. so check the bus below it. */
  578. pm_runtime_get_sync(&dev->dev);
  579. list_for_each_entry_safe_reverse(child, tmp, &bus->devices, bus_list)
  580. trim_stale_devices(child);
  581. pm_runtime_put(&dev->dev);
  582. }
  583. }
  584. /**
  585. * acpiphp_check_bridge - re-enumerate devices
  586. * @bridge: where to begin re-enumeration
  587. *
  588. * Iterate over all slots under this bridge and make sure that if a
  589. * card is present they are enabled, and if not they are disabled.
  590. */
  591. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  592. {
  593. struct acpiphp_slot *slot;
  594. /* Bail out if the bridge is going away. */
  595. if (bridge->is_going_away)
  596. return;
  597. if (bridge->pci_dev)
  598. pm_runtime_get_sync(&bridge->pci_dev->dev);
  599. list_for_each_entry(slot, &bridge->slots, node) {
  600. struct pci_bus *bus = slot->bus;
  601. struct pci_dev *dev, *tmp;
  602. if (slot_no_hotplug(slot)) {
  603. ; /* do nothing */
  604. } else if (device_status_valid(get_slot_status(slot))) {
  605. /* remove stale devices if any */
  606. list_for_each_entry_safe_reverse(dev, tmp,
  607. &bus->devices, bus_list)
  608. if (PCI_SLOT(dev->devfn) == slot->device)
  609. trim_stale_devices(dev);
  610. /* configure all functions */
  611. enable_slot(slot, true);
  612. } else {
  613. disable_slot(slot);
  614. }
  615. }
  616. if (bridge->pci_dev)
  617. pm_runtime_put(&bridge->pci_dev->dev);
  618. }
  619. /*
  620. * Remove devices for which we could not assign resources, call
  621. * arch specific code to fix-up the bus
  622. */
  623. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  624. {
  625. struct pci_dev *dev, *tmp;
  626. int i;
  627. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  628. list_for_each_entry_safe_reverse(dev, tmp, &bus->devices, bus_list) {
  629. for (i = 0; i < PCI_BRIDGE_RESOURCES; i++) {
  630. struct resource *res = &dev->resource[i];
  631. if ((res->flags & type_mask) && !res->start &&
  632. res->end) {
  633. /* Could not assign a required resources
  634. * for this device, remove it */
  635. pci_stop_and_remove_bus_device(dev);
  636. break;
  637. }
  638. }
  639. }
  640. }
  641. /*
  642. * ACPI event handlers
  643. */
  644. void acpiphp_check_host_bridge(struct acpi_device *adev)
  645. {
  646. struct acpiphp_bridge *bridge = NULL;
  647. acpi_lock_hp_context();
  648. if (adev->hp) {
  649. bridge = to_acpiphp_root_context(adev->hp)->root_bridge;
  650. if (bridge)
  651. get_bridge(bridge);
  652. }
  653. acpi_unlock_hp_context();
  654. if (bridge) {
  655. pci_lock_rescan_remove();
  656. acpiphp_check_bridge(bridge);
  657. pci_unlock_rescan_remove();
  658. put_bridge(bridge);
  659. }
  660. }
  661. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot);
  662. static void hotplug_event(u32 type, struct acpiphp_context *context)
  663. {
  664. acpi_handle handle = context->hp.self->handle;
  665. struct acpiphp_func *func = &context->func;
  666. struct acpiphp_slot *slot = func->slot;
  667. struct acpiphp_bridge *bridge;
  668. acpi_lock_hp_context();
  669. bridge = context->bridge;
  670. if (bridge)
  671. get_bridge(bridge);
  672. acpi_unlock_hp_context();
  673. pci_lock_rescan_remove();
  674. switch (type) {
  675. case ACPI_NOTIFY_BUS_CHECK:
  676. /* bus re-enumerate */
  677. acpi_handle_debug(handle, "Bus check in %s()\n", __func__);
  678. if (bridge)
  679. acpiphp_check_bridge(bridge);
  680. else if (!(slot->flags & SLOT_IS_GOING_AWAY))
  681. enable_slot(slot, false);
  682. break;
  683. case ACPI_NOTIFY_DEVICE_CHECK:
  684. /* device check */
  685. acpi_handle_debug(handle, "Device check in %s()\n", __func__);
  686. if (bridge) {
  687. acpiphp_check_bridge(bridge);
  688. } else if (!(slot->flags & SLOT_IS_GOING_AWAY)) {
  689. /*
  690. * Check if anything has changed in the slot and rescan
  691. * from the parent if that's the case.
  692. */
  693. if (acpiphp_rescan_slot(slot))
  694. acpiphp_check_bridge(func->parent);
  695. }
  696. break;
  697. case ACPI_NOTIFY_EJECT_REQUEST:
  698. /* request device eject */
  699. acpi_handle_debug(handle, "Eject request in %s()\n", __func__);
  700. acpiphp_disable_and_eject_slot(slot);
  701. break;
  702. }
  703. pci_unlock_rescan_remove();
  704. if (bridge)
  705. put_bridge(bridge);
  706. }
  707. static int acpiphp_hotplug_notify(struct acpi_device *adev, u32 type)
  708. {
  709. struct acpiphp_context *context;
  710. context = acpiphp_grab_context(adev);
  711. if (!context)
  712. return -ENODATA;
  713. hotplug_event(type, context);
  714. acpiphp_let_context_go(context);
  715. return 0;
  716. }
  717. /**
  718. * acpiphp_enumerate_slots - Enumerate PCI slots for a given bus.
  719. * @bus: PCI bus to enumerate the slots for.
  720. *
  721. * A "slot" is an object associated with a PCI device number. All functions
  722. * (PCI devices) with the same bus and device number belong to the same slot.
  723. */
  724. void acpiphp_enumerate_slots(struct pci_bus *bus)
  725. {
  726. struct acpiphp_bridge *bridge;
  727. struct acpi_device *adev;
  728. acpi_handle handle;
  729. acpi_status status;
  730. if (acpiphp_disabled)
  731. return;
  732. adev = ACPI_COMPANION(bus->bridge);
  733. if (!adev)
  734. return;
  735. handle = adev->handle;
  736. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  737. if (!bridge)
  738. return;
  739. INIT_LIST_HEAD(&bridge->slots);
  740. kref_init(&bridge->ref);
  741. bridge->pci_dev = pci_dev_get(bus->self);
  742. bridge->pci_bus = bus;
  743. /*
  744. * Grab a ref to the subordinate PCI bus in case the bus is
  745. * removed via PCI core logical hotplug. The ref pins the bus
  746. * (which we access during module unload).
  747. */
  748. get_device(&bus->dev);
  749. acpi_lock_hp_context();
  750. if (pci_is_root_bus(bridge->pci_bus)) {
  751. struct acpiphp_root_context *root_context;
  752. root_context = kzalloc(sizeof(*root_context), GFP_KERNEL);
  753. if (!root_context)
  754. goto err;
  755. root_context->root_bridge = bridge;
  756. acpi_set_hp_context(adev, &root_context->hp);
  757. } else {
  758. struct acpiphp_context *context;
  759. /*
  760. * This bridge should have been registered as a hotplug function
  761. * under its parent, so the context should be there, unless the
  762. * parent is going to be handled by pciehp, in which case this
  763. * bridge is not interesting to us either.
  764. */
  765. context = acpiphp_get_context(adev);
  766. if (!context)
  767. goto err;
  768. bridge->context = context;
  769. context->bridge = bridge;
  770. /* Get a reference to the parent bridge. */
  771. get_bridge(context->func.parent);
  772. }
  773. acpi_unlock_hp_context();
  774. /* Must be added to the list prior to calling acpiphp_add_context(). */
  775. mutex_lock(&bridge_mutex);
  776. list_add(&bridge->list, &bridge_list);
  777. mutex_unlock(&bridge_mutex);
  778. /* register all slot objects under this bridge */
  779. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  780. acpiphp_add_context, NULL, bridge, NULL);
  781. if (ACPI_FAILURE(status)) {
  782. acpi_handle_err(handle, "failed to register slots\n");
  783. cleanup_bridge(bridge);
  784. put_bridge(bridge);
  785. }
  786. return;
  787. err:
  788. acpi_unlock_hp_context();
  789. put_device(&bus->dev);
  790. pci_dev_put(bridge->pci_dev);
  791. kfree(bridge);
  792. }
  793. static void acpiphp_drop_bridge(struct acpiphp_bridge *bridge)
  794. {
  795. if (pci_is_root_bus(bridge->pci_bus)) {
  796. struct acpiphp_root_context *root_context;
  797. struct acpi_device *adev;
  798. acpi_lock_hp_context();
  799. adev = ACPI_COMPANION(bridge->pci_bus->bridge);
  800. root_context = to_acpiphp_root_context(adev->hp);
  801. adev->hp = NULL;
  802. acpi_unlock_hp_context();
  803. kfree(root_context);
  804. }
  805. cleanup_bridge(bridge);
  806. put_bridge(bridge);
  807. }
  808. /**
  809. * acpiphp_remove_slots - Remove slot objects associated with a given bus.
  810. * @bus: PCI bus to remove the slot objects for.
  811. */
  812. void acpiphp_remove_slots(struct pci_bus *bus)
  813. {
  814. struct acpiphp_bridge *bridge;
  815. if (acpiphp_disabled)
  816. return;
  817. mutex_lock(&bridge_mutex);
  818. list_for_each_entry(bridge, &bridge_list, list)
  819. if (bridge->pci_bus == bus) {
  820. mutex_unlock(&bridge_mutex);
  821. acpiphp_drop_bridge(bridge);
  822. return;
  823. }
  824. mutex_unlock(&bridge_mutex);
  825. }
  826. /**
  827. * acpiphp_enable_slot - power on slot
  828. * @slot: ACPI PHP slot
  829. */
  830. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  831. {
  832. pci_lock_rescan_remove();
  833. if (slot->flags & SLOT_IS_GOING_AWAY) {
  834. pci_unlock_rescan_remove();
  835. return -ENODEV;
  836. }
  837. /* configure all functions */
  838. if (!(slot->flags & SLOT_ENABLED))
  839. enable_slot(slot, false);
  840. pci_unlock_rescan_remove();
  841. return 0;
  842. }
  843. /**
  844. * acpiphp_disable_and_eject_slot - power off and eject slot
  845. * @slot: ACPI PHP slot
  846. */
  847. static int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  848. {
  849. struct acpiphp_func *func;
  850. if (slot->flags & SLOT_IS_GOING_AWAY)
  851. return -ENODEV;
  852. /* unconfigure all functions */
  853. disable_slot(slot);
  854. list_for_each_entry(func, &slot->funcs, sibling)
  855. if (func->flags & FUNC_HAS_EJ0) {
  856. acpi_handle handle = func_to_handle(func);
  857. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  858. acpi_handle_err(handle, "_EJ0 failed\n");
  859. break;
  860. }
  861. return 0;
  862. }
  863. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  864. {
  865. int ret;
  866. /*
  867. * Acquire acpi_scan_lock to ensure that the execution of _EJ0 in
  868. * acpiphp_disable_and_eject_slot() will be synchronized properly.
  869. */
  870. acpi_scan_lock_acquire();
  871. pci_lock_rescan_remove();
  872. ret = acpiphp_disable_and_eject_slot(slot);
  873. pci_unlock_rescan_remove();
  874. acpi_scan_lock_release();
  875. return ret;
  876. }
  877. /*
  878. * slot enabled: 1
  879. * slot disabled: 0
  880. */
  881. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  882. {
  883. return (slot->flags & SLOT_ENABLED);
  884. }
  885. /*
  886. * latch open: 1
  887. * latch closed: 0
  888. */
  889. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  890. {
  891. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  892. }
  893. /*
  894. * adapter presence : 1
  895. * absence : 0
  896. */
  897. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  898. {
  899. return !!get_slot_status(slot);
  900. }