acpiphp_glue.c 26 KB

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