acpiphp_glue.c 31 KB

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