acpiphp_glue.c 26 KB

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