acpiphp_glue.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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. #include <linux/init.h>
  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 DEFINE_MUTEX(acpiphp_context_lock);
  55. #define MY_NAME "acpiphp_glue"
  56. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
  57. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  58. static void acpiphp_set_hpp_values(struct pci_bus *bus);
  59. static void hotplug_event(acpi_handle handle, u32 type, void *data);
  60. static void free_bridge(struct kref *kref);
  61. static void acpiphp_context_handler(acpi_handle handle, void *context)
  62. {
  63. /* Intentionally empty. */
  64. }
  65. /**
  66. * acpiphp_init_context - Create hotplug context and grab a reference to it.
  67. * @handle: ACPI object handle to create the context for.
  68. *
  69. * Call under acpiphp_context_lock.
  70. */
  71. static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
  72. {
  73. struct acpiphp_context *context;
  74. acpi_status status;
  75. context = kzalloc(sizeof(*context), GFP_KERNEL);
  76. if (!context)
  77. return NULL;
  78. context->handle = handle;
  79. context->refcount = 1;
  80. status = acpi_attach_data(handle, acpiphp_context_handler, context);
  81. if (ACPI_FAILURE(status)) {
  82. kfree(context);
  83. return NULL;
  84. }
  85. return context;
  86. }
  87. /**
  88. * acpiphp_get_context - Get hotplug context and grab a reference to it.
  89. * @handle: ACPI object handle to get the context for.
  90. *
  91. * Call under acpiphp_context_lock.
  92. */
  93. static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
  94. {
  95. struct acpiphp_context *context = NULL;
  96. acpi_status status;
  97. void *data;
  98. status = acpi_get_data(handle, acpiphp_context_handler, &data);
  99. if (ACPI_SUCCESS(status)) {
  100. context = data;
  101. context->refcount++;
  102. }
  103. return context;
  104. }
  105. /**
  106. * acpiphp_put_context - Drop a reference to ACPI hotplug context.
  107. * @handle: ACPI object handle to put the context for.
  108. *
  109. * The context object is removed if there are no more references to it.
  110. *
  111. * Call under acpiphp_context_lock.
  112. */
  113. static void acpiphp_put_context(struct acpiphp_context *context)
  114. {
  115. if (--context->refcount)
  116. return;
  117. WARN_ON(context->bridge);
  118. acpi_detach_data(context->handle, acpiphp_context_handler);
  119. kfree(context);
  120. }
  121. static inline void get_bridge(struct acpiphp_bridge *bridge)
  122. {
  123. kref_get(&bridge->ref);
  124. }
  125. static inline void put_bridge(struct acpiphp_bridge *bridge)
  126. {
  127. kref_put(&bridge->ref, free_bridge);
  128. }
  129. static void free_bridge(struct kref *kref)
  130. {
  131. struct acpiphp_context *context;
  132. struct acpiphp_bridge *bridge;
  133. struct acpiphp_slot *slot, *next;
  134. struct acpiphp_func *func, *tmp;
  135. mutex_lock(&acpiphp_context_lock);
  136. bridge = container_of(kref, struct acpiphp_bridge, ref);
  137. list_for_each_entry_safe(slot, next, &bridge->slots, node) {
  138. list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
  139. acpiphp_put_context(func_to_context(func));
  140. kfree(slot);
  141. }
  142. context = bridge->context;
  143. /* Root bridges will not have hotplug context. */
  144. if (context) {
  145. /* Release the reference taken by acpiphp_enumerate_slots(). */
  146. put_bridge(context->func.parent);
  147. context->bridge = NULL;
  148. acpiphp_put_context(context);
  149. }
  150. put_device(&bridge->pci_bus->dev);
  151. pci_dev_put(bridge->pci_dev);
  152. kfree(bridge);
  153. mutex_unlock(&acpiphp_context_lock);
  154. }
  155. /*
  156. * the _DCK method can do funny things... and sometimes not
  157. * hah-hah funny.
  158. *
  159. * TBD - figure out a way to only call fixups for
  160. * systems that require them.
  161. */
  162. static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
  163. {
  164. struct acpiphp_context *context = data;
  165. struct pci_bus *bus = context->func.slot->bus;
  166. u32 buses;
  167. if (!bus->self)
  168. return;
  169. /* fixup bad _DCK function that rewrites
  170. * secondary bridge on slot
  171. */
  172. pci_read_config_dword(bus->self,
  173. PCI_PRIMARY_BUS,
  174. &buses);
  175. if (((buses >> 8) & 0xff) != bus->busn_res.start) {
  176. buses = (buses & 0xff000000)
  177. | ((unsigned int)(bus->primary) << 0)
  178. | ((unsigned int)(bus->busn_res.start) << 8)
  179. | ((unsigned int)(bus->busn_res.end) << 16);
  180. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  181. }
  182. }
  183. static const struct acpi_dock_ops acpiphp_dock_ops = {
  184. .fixup = post_dock_fixups,
  185. .handler = hotplug_event,
  186. };
  187. /* Check whether the PCI device is managed by native PCIe hotplug driver */
  188. static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
  189. {
  190. u32 reg32;
  191. acpi_handle tmp;
  192. struct acpi_pci_root *root;
  193. /* Check whether the PCIe port supports native PCIe hotplug */
  194. if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &reg32))
  195. return false;
  196. if (!(reg32 & PCI_EXP_SLTCAP_HPC))
  197. return false;
  198. /*
  199. * Check whether native PCIe hotplug has been enabled for
  200. * this PCIe hierarchy.
  201. */
  202. tmp = acpi_find_root_bridge_handle(pdev);
  203. if (!tmp)
  204. return false;
  205. root = acpi_pci_find_root(tmp);
  206. if (!root)
  207. return false;
  208. if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
  209. return false;
  210. return true;
  211. }
  212. static void acpiphp_dock_init(void *data)
  213. {
  214. struct acpiphp_context *context = data;
  215. get_bridge(context->func.parent);
  216. }
  217. static void acpiphp_dock_release(void *data)
  218. {
  219. struct acpiphp_context *context = data;
  220. put_bridge(context->func.parent);
  221. }
  222. /* callback routine to register each ACPI PCI slot object */
  223. static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
  224. void **rv)
  225. {
  226. struct acpiphp_bridge *bridge = data;
  227. struct acpiphp_context *context;
  228. struct acpiphp_slot *slot;
  229. struct acpiphp_func *newfunc;
  230. acpi_status status = AE_OK;
  231. unsigned long long adr;
  232. int device, function;
  233. struct pci_bus *pbus = bridge->pci_bus;
  234. struct pci_dev *pdev = bridge->pci_dev;
  235. u32 val;
  236. if (pdev && device_is_managed_by_native_pciehp(pdev))
  237. return AE_OK;
  238. status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  239. if (ACPI_FAILURE(status)) {
  240. acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
  241. return AE_OK;
  242. }
  243. device = (adr >> 16) & 0xffff;
  244. function = adr & 0xffff;
  245. mutex_lock(&acpiphp_context_lock);
  246. context = acpiphp_init_context(handle);
  247. if (!context) {
  248. mutex_unlock(&acpiphp_context_lock);
  249. acpi_handle_err(handle, "No hotplug context\n");
  250. return AE_NOT_EXIST;
  251. }
  252. newfunc = &context->func;
  253. newfunc->function = function;
  254. newfunc->parent = bridge;
  255. mutex_unlock(&acpiphp_context_lock);
  256. if (acpi_has_method(handle, "_EJ0"))
  257. newfunc->flags = FUNC_HAS_EJ0;
  258. if (acpi_has_method(handle, "_STA"))
  259. newfunc->flags |= FUNC_HAS_STA;
  260. if (acpi_has_method(handle, "_DCK"))
  261. newfunc->flags |= FUNC_HAS_DCK;
  262. /* search for objects that share the same slot */
  263. list_for_each_entry(slot, &bridge->slots, node)
  264. if (slot->device == device)
  265. goto slot_found;
  266. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  267. if (!slot) {
  268. status = AE_NO_MEMORY;
  269. goto err;
  270. }
  271. slot->bus = bridge->pci_bus;
  272. slot->device = device;
  273. INIT_LIST_HEAD(&slot->funcs);
  274. mutex_init(&slot->crit_sect);
  275. mutex_lock(&bridge_mutex);
  276. list_add_tail(&slot->node, &bridge->slots);
  277. mutex_unlock(&bridge_mutex);
  278. /* Register slots for ejectable funtions only. */
  279. if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
  280. unsigned long long sun;
  281. int retval;
  282. bridge->nr_slots++;
  283. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  284. if (ACPI_FAILURE(status))
  285. sun = bridge->nr_slots;
  286. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  287. sun, pci_domain_nr(pbus), pbus->number, device);
  288. retval = acpiphp_register_hotplug_slot(slot, sun);
  289. if (retval) {
  290. bridge->nr_slots--;
  291. if (retval == -EBUSY)
  292. warn("Slot %llu already registered by another "
  293. "hotplug driver\n", sun);
  294. else
  295. warn("acpiphp_register_hotplug_slot failed "
  296. "(err code = 0x%x)\n", retval);
  297. }
  298. /* Even if the slot registration fails, we can still use it. */
  299. }
  300. slot_found:
  301. newfunc->slot = slot;
  302. mutex_lock(&bridge_mutex);
  303. list_add_tail(&newfunc->sibling, &slot->funcs);
  304. mutex_unlock(&bridge_mutex);
  305. if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
  306. &val, 60*1000))
  307. slot->flags |= SLOT_ENABLED;
  308. if (is_dock_device(handle)) {
  309. /* we don't want to call this device's _EJ0
  310. * because we want the dock notify handler
  311. * to call it after it calls _DCK
  312. */
  313. newfunc->flags &= ~FUNC_HAS_EJ0;
  314. if (register_hotplug_dock_device(handle,
  315. &acpiphp_dock_ops, context,
  316. acpiphp_dock_init, acpiphp_dock_release))
  317. dbg("failed to register dock device\n");
  318. }
  319. /* install notify handler */
  320. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  321. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  322. handle_hotplug_event,
  323. context);
  324. if (ACPI_FAILURE(status))
  325. acpi_handle_err(handle,
  326. "failed to install notify handler\n");
  327. }
  328. return AE_OK;
  329. err:
  330. mutex_lock(&acpiphp_context_lock);
  331. acpiphp_put_context(context);
  332. mutex_unlock(&acpiphp_context_lock);
  333. return status;
  334. }
  335. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  336. {
  337. struct acpiphp_context *context;
  338. struct acpiphp_bridge *bridge = NULL;
  339. mutex_lock(&acpiphp_context_lock);
  340. context = acpiphp_get_context(handle);
  341. if (context) {
  342. bridge = context->bridge;
  343. if (bridge)
  344. get_bridge(bridge);
  345. acpiphp_put_context(context);
  346. }
  347. mutex_unlock(&acpiphp_context_lock);
  348. return bridge;
  349. }
  350. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  351. {
  352. struct acpiphp_slot *slot;
  353. struct acpiphp_func *func;
  354. acpi_status status;
  355. list_for_each_entry(slot, &bridge->slots, node) {
  356. list_for_each_entry(func, &slot->funcs, sibling) {
  357. acpi_handle handle = func_to_handle(func);
  358. if (is_dock_device(handle))
  359. unregister_hotplug_dock_device(handle);
  360. if (!(func->flags & FUNC_HAS_DCK)) {
  361. status = acpi_remove_notify_handler(handle,
  362. ACPI_SYSTEM_NOTIFY,
  363. handle_hotplug_event);
  364. if (ACPI_FAILURE(status))
  365. err("failed to remove notify handler\n");
  366. }
  367. }
  368. acpiphp_unregister_hotplug_slot(slot);
  369. }
  370. mutex_lock(&bridge_mutex);
  371. list_del(&bridge->list);
  372. mutex_unlock(&bridge_mutex);
  373. }
  374. /**
  375. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  376. * @bus: bus to start search with
  377. */
  378. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  379. {
  380. struct list_head *tmp;
  381. unsigned char max, n;
  382. /*
  383. * pci_bus_max_busnr will return the highest
  384. * reserved busnr for all these children.
  385. * that is equivalent to the bus->subordinate
  386. * value. We don't want to use the parent's
  387. * bus->subordinate value because it could have
  388. * padding in it.
  389. */
  390. max = bus->busn_res.start;
  391. list_for_each(tmp, &bus->children) {
  392. n = pci_bus_max_busnr(pci_bus_b(tmp));
  393. if (n > max)
  394. max = n;
  395. }
  396. return max;
  397. }
  398. /**
  399. * acpiphp_bus_trim - Trim device objects in an ACPI namespace subtree.
  400. * @handle: ACPI device object handle to start from.
  401. */
  402. static void acpiphp_bus_trim(acpi_handle handle)
  403. {
  404. struct acpi_device *adev = NULL;
  405. acpi_bus_get_device(handle, &adev);
  406. if (adev)
  407. acpi_bus_trim(adev);
  408. }
  409. /**
  410. * acpiphp_bus_add - Scan ACPI namespace subtree.
  411. * @handle: ACPI object handle to start the scan from.
  412. */
  413. static void acpiphp_bus_add(acpi_handle handle)
  414. {
  415. struct acpi_device *adev = NULL;
  416. acpiphp_bus_trim(handle);
  417. acpi_bus_scan(handle);
  418. acpi_bus_get_device(handle, &adev);
  419. if (adev)
  420. acpi_device_set_power(adev, ACPI_STATE_D0);
  421. }
  422. static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
  423. {
  424. struct acpiphp_func *func;
  425. union acpi_object params[2];
  426. struct acpi_object_list arg_list;
  427. list_for_each_entry(func, &slot->funcs, sibling) {
  428. arg_list.count = 2;
  429. arg_list.pointer = params;
  430. params[0].type = ACPI_TYPE_INTEGER;
  431. params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
  432. params[1].type = ACPI_TYPE_INTEGER;
  433. params[1].integer.value = 1;
  434. /* _REG is optional, we don't care about if there is failure */
  435. acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
  436. NULL);
  437. }
  438. }
  439. static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
  440. {
  441. struct acpiphp_func *func;
  442. /* quirk, or pcie could set it already */
  443. if (dev->is_hotplug_bridge)
  444. return;
  445. list_for_each_entry(func, &slot->funcs, sibling) {
  446. if (PCI_FUNC(dev->devfn) == func->function) {
  447. dev->is_hotplug_bridge = 1;
  448. break;
  449. }
  450. }
  451. }
  452. /**
  453. * enable_device - enable, configure a slot
  454. * @slot: slot to be enabled
  455. *
  456. * This function should be called per *physical slot*,
  457. * not per each slot object in ACPI namespace.
  458. */
  459. static int __ref enable_device(struct acpiphp_slot *slot)
  460. {
  461. struct pci_dev *dev;
  462. struct pci_bus *bus = slot->bus;
  463. struct acpiphp_func *func;
  464. int max, pass;
  465. LIST_HEAD(add_list);
  466. list_for_each_entry(func, &slot->funcs, sibling)
  467. acpiphp_bus_add(func_to_handle(func));
  468. pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  469. max = acpiphp_max_busnr(bus);
  470. for (pass = 0; pass < 2; pass++) {
  471. list_for_each_entry(dev, &bus->devices, bus_list) {
  472. if (PCI_SLOT(dev->devfn) != slot->device)
  473. continue;
  474. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  475. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  476. max = pci_scan_bridge(bus, dev, max, pass);
  477. if (pass && dev->subordinate) {
  478. check_hotplug_bridge(slot, dev);
  479. pcibios_resource_survey_bus(dev->subordinate);
  480. __pci_bus_size_bridges(dev->subordinate,
  481. &add_list);
  482. }
  483. }
  484. }
  485. }
  486. __pci_bus_assign_resources(bus, &add_list, NULL);
  487. acpiphp_sanitize_bus(bus);
  488. acpiphp_set_hpp_values(bus);
  489. acpiphp_set_acpi_region(slot);
  490. pci_enable_bridges(bus);
  491. list_for_each_entry(dev, &bus->devices, bus_list) {
  492. /* Assume that newly added devices are powered on already. */
  493. if (!dev->is_added)
  494. dev->current_state = PCI_D0;
  495. }
  496. pci_bus_add_devices(bus);
  497. slot->flags |= SLOT_ENABLED;
  498. list_for_each_entry(func, &slot->funcs, sibling) {
  499. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  500. func->function));
  501. if (!dev) {
  502. /* Do not set SLOT_ENABLED flag if some funcs
  503. are not added. */
  504. slot->flags &= (~SLOT_ENABLED);
  505. continue;
  506. }
  507. }
  508. return 0;
  509. }
  510. /* return first device in slot, acquiring a reference on it */
  511. static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
  512. {
  513. struct pci_bus *bus = slot->bus;
  514. struct pci_dev *dev;
  515. struct pci_dev *ret = NULL;
  516. down_read(&pci_bus_sem);
  517. list_for_each_entry(dev, &bus->devices, bus_list)
  518. if (PCI_SLOT(dev->devfn) == slot->device) {
  519. ret = pci_dev_get(dev);
  520. break;
  521. }
  522. up_read(&pci_bus_sem);
  523. return ret;
  524. }
  525. /**
  526. * disable_device - disable a slot
  527. * @slot: ACPI PHP slot
  528. */
  529. static int disable_device(struct acpiphp_slot *slot)
  530. {
  531. struct acpiphp_func *func;
  532. struct pci_dev *pdev;
  533. /*
  534. * enable_device() enumerates all functions in this device via
  535. * pci_scan_slot(), whether they have associated ACPI hotplug
  536. * methods (_EJ0, etc.) or not. Therefore, we remove all functions
  537. * here.
  538. */
  539. while ((pdev = dev_in_slot(slot))) {
  540. pci_stop_and_remove_bus_device(pdev);
  541. pci_dev_put(pdev);
  542. }
  543. list_for_each_entry(func, &slot->funcs, sibling)
  544. acpiphp_bus_trim(func_to_handle(func));
  545. slot->flags &= (~SLOT_ENABLED);
  546. return 0;
  547. }
  548. /**
  549. * get_slot_status - get ACPI slot status
  550. * @slot: ACPI PHP slot
  551. *
  552. * If a slot has _STA for each function and if any one of them
  553. * returned non-zero status, return it.
  554. *
  555. * If a slot doesn't have _STA and if any one of its functions'
  556. * configuration space is configured, return 0x0f as a _STA.
  557. *
  558. * Otherwise return 0.
  559. */
  560. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  561. {
  562. unsigned long long sta = 0;
  563. struct acpiphp_func *func;
  564. list_for_each_entry(func, &slot->funcs, sibling) {
  565. if (func->flags & FUNC_HAS_STA) {
  566. acpi_status status;
  567. status = acpi_evaluate_integer(func_to_handle(func),
  568. "_STA", NULL, &sta);
  569. if (ACPI_SUCCESS(status) && sta)
  570. break;
  571. } else {
  572. u32 dvid;
  573. pci_bus_read_config_dword(slot->bus,
  574. PCI_DEVFN(slot->device,
  575. func->function),
  576. PCI_VENDOR_ID, &dvid);
  577. if (dvid != 0xffffffff) {
  578. sta = ACPI_STA_ALL;
  579. break;
  580. }
  581. }
  582. }
  583. return (unsigned int)sta;
  584. }
  585. /**
  586. * trim_stale_devices - remove PCI devices that are not responding.
  587. * @dev: PCI device to start walking the hierarchy from.
  588. */
  589. static void trim_stale_devices(struct pci_dev *dev)
  590. {
  591. acpi_handle handle = ACPI_HANDLE(&dev->dev);
  592. struct pci_bus *bus = dev->subordinate;
  593. bool alive = false;
  594. if (handle) {
  595. acpi_status status;
  596. unsigned long long sta;
  597. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  598. alive = ACPI_SUCCESS(status) && sta == ACPI_STA_ALL;
  599. }
  600. if (!alive) {
  601. u32 v;
  602. /* Check if the device responds. */
  603. alive = pci_bus_read_dev_vendor_id(dev->bus, dev->devfn, &v, 0);
  604. }
  605. if (!alive) {
  606. pci_stop_and_remove_bus_device(dev);
  607. if (handle)
  608. acpiphp_bus_trim(handle);
  609. } else if (bus) {
  610. struct pci_dev *child, *tmp;
  611. /* The device is a bridge. so check the bus below it. */
  612. pm_runtime_get_sync(&dev->dev);
  613. list_for_each_entry_safe(child, tmp, &bus->devices, bus_list)
  614. trim_stale_devices(child);
  615. pm_runtime_put(&dev->dev);
  616. }
  617. }
  618. /**
  619. * acpiphp_check_bridge - re-enumerate devices
  620. * @bridge: where to begin re-enumeration
  621. *
  622. * Iterate over all slots under this bridge and make sure that if a
  623. * card is present they are enabled, and if not they are disabled.
  624. */
  625. static void acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  626. {
  627. struct acpiphp_slot *slot;
  628. list_for_each_entry(slot, &bridge->slots, node) {
  629. struct pci_bus *bus = slot->bus;
  630. struct pci_dev *dev, *tmp;
  631. mutex_lock(&slot->crit_sect);
  632. /* wake up all functions */
  633. if (get_slot_status(slot) == ACPI_STA_ALL) {
  634. /* remove stale devices if any */
  635. list_for_each_entry_safe(dev, tmp, &bus->devices,
  636. bus_list)
  637. if (PCI_SLOT(dev->devfn) == slot->device)
  638. trim_stale_devices(dev);
  639. /* configure all functions */
  640. enable_device(slot);
  641. } else {
  642. disable_device(slot);
  643. }
  644. mutex_unlock(&slot->crit_sect);
  645. }
  646. }
  647. static void acpiphp_set_hpp_values(struct pci_bus *bus)
  648. {
  649. struct pci_dev *dev;
  650. list_for_each_entry(dev, &bus->devices, bus_list)
  651. pci_configure_slot(dev);
  652. }
  653. /*
  654. * Remove devices for which we could not assign resources, call
  655. * arch specific code to fix-up the bus
  656. */
  657. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  658. {
  659. struct pci_dev *dev, *tmp;
  660. int i;
  661. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  662. list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
  663. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  664. struct resource *res = &dev->resource[i];
  665. if ((res->flags & type_mask) && !res->start &&
  666. res->end) {
  667. /* Could not assign a required resources
  668. * for this device, remove it */
  669. pci_stop_and_remove_bus_device(dev);
  670. break;
  671. }
  672. }
  673. }
  674. }
  675. /*
  676. * ACPI event handlers
  677. */
  678. static acpi_status
  679. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  680. {
  681. struct acpiphp_bridge *bridge;
  682. char objname[64];
  683. struct acpi_buffer buffer = { .length = sizeof(objname),
  684. .pointer = objname };
  685. bridge = acpiphp_handle_to_bridge(handle);
  686. if (bridge) {
  687. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  688. dbg("%s: re-enumerating slots under %s\n",
  689. __func__, objname);
  690. acpiphp_check_bridge(bridge);
  691. put_bridge(bridge);
  692. }
  693. return AE_OK ;
  694. }
  695. void acpiphp_check_host_bridge(acpi_handle handle)
  696. {
  697. struct acpiphp_bridge *bridge;
  698. bridge = acpiphp_handle_to_bridge(handle);
  699. if (bridge) {
  700. acpiphp_check_bridge(bridge);
  701. put_bridge(bridge);
  702. }
  703. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  704. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
  705. }
  706. static void hotplug_event(acpi_handle handle, u32 type, void *data)
  707. {
  708. struct acpiphp_context *context = data;
  709. struct acpiphp_func *func = &context->func;
  710. struct acpiphp_bridge *bridge;
  711. char objname[64];
  712. struct acpi_buffer buffer = { .length = sizeof(objname),
  713. .pointer = objname };
  714. mutex_lock(&acpiphp_context_lock);
  715. bridge = context->bridge;
  716. if (bridge)
  717. get_bridge(bridge);
  718. mutex_unlock(&acpiphp_context_lock);
  719. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  720. switch (type) {
  721. case ACPI_NOTIFY_BUS_CHECK:
  722. /* bus re-enumerate */
  723. dbg("%s: Bus check notify on %s\n", __func__, objname);
  724. dbg("%s: re-enumerating slots under %s\n", __func__, objname);
  725. if (bridge) {
  726. acpiphp_check_bridge(bridge);
  727. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  728. ACPI_UINT32_MAX, check_sub_bridges,
  729. NULL, NULL, NULL);
  730. } else {
  731. struct acpiphp_slot *slot = func->slot;
  732. mutex_lock(&slot->crit_sect);
  733. enable_device(slot);
  734. mutex_unlock(&slot->crit_sect);
  735. }
  736. break;
  737. case ACPI_NOTIFY_DEVICE_CHECK:
  738. /* device check */
  739. dbg("%s: Device check notify on %s\n", __func__, objname);
  740. if (bridge)
  741. acpiphp_check_bridge(bridge);
  742. else
  743. acpiphp_check_bridge(func->parent);
  744. break;
  745. case ACPI_NOTIFY_EJECT_REQUEST:
  746. /* request device eject */
  747. dbg("%s: Device eject notify on %s\n", __func__, objname);
  748. acpiphp_disable_and_eject_slot(func->slot);
  749. break;
  750. }
  751. if (bridge)
  752. put_bridge(bridge);
  753. }
  754. static void hotplug_event_work(struct work_struct *work)
  755. {
  756. struct acpiphp_context *context;
  757. struct acpi_hp_work *hp_work;
  758. hp_work = container_of(work, struct acpi_hp_work, work);
  759. context = hp_work->context;
  760. acpi_scan_lock_acquire();
  761. hotplug_event(hp_work->handle, hp_work->type, context);
  762. acpi_scan_lock_release();
  763. kfree(hp_work); /* allocated in handle_hotplug_event() */
  764. put_bridge(context->func.parent);
  765. }
  766. /**
  767. * handle_hotplug_event - handle ACPI hotplug event
  768. * @handle: Notify()'ed acpi_handle
  769. * @type: Notify code
  770. * @data: pointer to acpiphp_context structure
  771. *
  772. * Handles ACPI event notification on slots.
  773. */
  774. static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
  775. {
  776. struct acpiphp_context *context;
  777. switch (type) {
  778. case ACPI_NOTIFY_BUS_CHECK:
  779. case ACPI_NOTIFY_DEVICE_CHECK:
  780. case ACPI_NOTIFY_EJECT_REQUEST:
  781. break;
  782. case ACPI_NOTIFY_DEVICE_WAKE:
  783. return;
  784. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  785. acpi_handle_err(handle, "Device cannot be configured due "
  786. "to a frequency mismatch\n");
  787. return;
  788. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  789. acpi_handle_err(handle, "Device cannot be configured due "
  790. "to a bus mode mismatch\n");
  791. return;
  792. case ACPI_NOTIFY_POWER_FAULT:
  793. acpi_handle_err(handle, "Device has suffered a power fault\n");
  794. return;
  795. default:
  796. acpi_handle_warn(handle, "Unsupported event type 0x%x\n", type);
  797. return;
  798. }
  799. mutex_lock(&acpiphp_context_lock);
  800. context = acpiphp_get_context(handle);
  801. if (context) {
  802. get_bridge(context->func.parent);
  803. acpiphp_put_context(context);
  804. alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
  805. }
  806. mutex_unlock(&acpiphp_context_lock);
  807. }
  808. /*
  809. * Create hotplug slots for the PCI bus.
  810. * It should always return 0 to avoid skipping following notifiers.
  811. */
  812. void acpiphp_enumerate_slots(struct pci_bus *bus)
  813. {
  814. struct acpiphp_bridge *bridge;
  815. acpi_handle handle;
  816. acpi_status status;
  817. if (acpiphp_disabled)
  818. return;
  819. handle = ACPI_HANDLE(bus->bridge);
  820. if (!handle)
  821. return;
  822. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  823. if (!bridge) {
  824. acpi_handle_err(handle, "No memory for bridge object\n");
  825. return;
  826. }
  827. INIT_LIST_HEAD(&bridge->slots);
  828. kref_init(&bridge->ref);
  829. bridge->pci_dev = pci_dev_get(bus->self);
  830. bridge->pci_bus = bus;
  831. /*
  832. * Grab a ref to the subordinate PCI bus in case the bus is
  833. * removed via PCI core logical hotplug. The ref pins the bus
  834. * (which we access during module unload).
  835. */
  836. get_device(&bus->dev);
  837. if (!pci_is_root_bus(bridge->pci_bus)) {
  838. struct acpiphp_context *context;
  839. /*
  840. * This bridge should have been registered as a hotplug function
  841. * under its parent, so the context has to be there. If not, we
  842. * are in deep goo.
  843. */
  844. mutex_lock(&acpiphp_context_lock);
  845. context = acpiphp_get_context(handle);
  846. if (WARN_ON(!context)) {
  847. mutex_unlock(&acpiphp_context_lock);
  848. put_device(&bus->dev);
  849. kfree(bridge);
  850. return;
  851. }
  852. bridge->context = context;
  853. context->bridge = bridge;
  854. /* Get a reference to the parent bridge. */
  855. get_bridge(context->func.parent);
  856. mutex_unlock(&acpiphp_context_lock);
  857. }
  858. /* must be added to the list prior to calling register_slot */
  859. mutex_lock(&bridge_mutex);
  860. list_add(&bridge->list, &bridge_list);
  861. mutex_unlock(&bridge_mutex);
  862. /* register all slot objects under this bridge */
  863. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
  864. register_slot, NULL, bridge, NULL);
  865. if (ACPI_FAILURE(status)) {
  866. acpi_handle_err(handle, "failed to register slots\n");
  867. cleanup_bridge(bridge);
  868. put_bridge(bridge);
  869. }
  870. }
  871. /* Destroy hotplug slots associated with the PCI bus */
  872. void acpiphp_remove_slots(struct pci_bus *bus)
  873. {
  874. struct acpiphp_bridge *bridge, *tmp;
  875. if (acpiphp_disabled)
  876. return;
  877. list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
  878. if (bridge->pci_bus == bus) {
  879. cleanup_bridge(bridge);
  880. put_bridge(bridge);
  881. break;
  882. }
  883. }
  884. /**
  885. * acpiphp_enable_slot - power on slot
  886. * @slot: ACPI PHP slot
  887. */
  888. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  889. {
  890. int retval = 0;
  891. mutex_lock(&slot->crit_sect);
  892. /* configure all functions */
  893. if (!(slot->flags & SLOT_ENABLED))
  894. retval = enable_device(slot);
  895. mutex_unlock(&slot->crit_sect);
  896. return retval;
  897. }
  898. /**
  899. * acpiphp_disable_and_eject_slot - power off and eject slot
  900. * @slot: ACPI PHP slot
  901. */
  902. int acpiphp_disable_and_eject_slot(struct acpiphp_slot *slot)
  903. {
  904. struct acpiphp_func *func;
  905. int retval = 0;
  906. mutex_lock(&slot->crit_sect);
  907. /* unconfigure all functions */
  908. retval = disable_device(slot);
  909. if (retval)
  910. goto err_exit;
  911. list_for_each_entry(func, &slot->funcs, sibling)
  912. if (func->flags & FUNC_HAS_EJ0) {
  913. acpi_handle handle = func_to_handle(func);
  914. if (ACPI_FAILURE(acpi_evaluate_ej0(handle)))
  915. acpi_handle_err(handle, "_EJ0 failed\n");
  916. break;
  917. }
  918. err_exit:
  919. mutex_unlock(&slot->crit_sect);
  920. return retval;
  921. }
  922. /*
  923. * slot enabled: 1
  924. * slot disabled: 0
  925. */
  926. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  927. {
  928. return (slot->flags & SLOT_ENABLED);
  929. }
  930. /*
  931. * latch open: 1
  932. * latch closed: 0
  933. */
  934. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  935. {
  936. return !(get_slot_status(slot) & ACPI_STA_DEVICE_UI);
  937. }
  938. /*
  939. * adapter presence : 1
  940. * absence : 0
  941. */
  942. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  943. {
  944. return !!get_slot_status(slot);
  945. }