acpiphp_glue.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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 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_obj(*context);
  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_obj(struct acpiphp_slot);
  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. /*
  346. * In the PCIe case, only Root Ports and Downstream Ports are capable of
  347. * accommodating hotplug devices, so avoid marking Upstream Ports as
  348. * "hotplug bridges".
  349. */
  350. if (pci_is_pcie(dev) && pci_pcie_type(dev) == PCI_EXP_TYPE_UPSTREAM)
  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_obj(struct acpiphp_bridge);
  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_obj(*root_context);
  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. }