acpiphp_glue.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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 "../pci.h"
  48. #include "acpiphp.h"
  49. static LIST_HEAD(bridge_list);
  50. static LIST_HEAD(ioapic_list);
  51. static DEFINE_SPINLOCK(ioapic_list_lock);
  52. #define MY_NAME "acpiphp_glue"
  53. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  54. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  55. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
  56. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  57. /* callback routine to check for the existence of a pci dock device */
  58. static acpi_status
  59. is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
  60. {
  61. int *count = (int *)context;
  62. if (is_dock_device(handle)) {
  63. (*count)++;
  64. return AE_CTRL_TERMINATE;
  65. } else {
  66. return AE_OK;
  67. }
  68. }
  69. /*
  70. * the _DCK method can do funny things... and sometimes not
  71. * hah-hah funny.
  72. *
  73. * TBD - figure out a way to only call fixups for
  74. * systems that require them.
  75. */
  76. static int post_dock_fixups(struct notifier_block *nb, unsigned long val,
  77. void *v)
  78. {
  79. struct acpiphp_func *func = container_of(nb, struct acpiphp_func, nb);
  80. struct pci_bus *bus = func->slot->bridge->pci_bus;
  81. u32 buses;
  82. if (!bus->self)
  83. return NOTIFY_OK;
  84. /* fixup bad _DCK function that rewrites
  85. * secondary bridge on slot
  86. */
  87. pci_read_config_dword(bus->self,
  88. PCI_PRIMARY_BUS,
  89. &buses);
  90. if (((buses >> 8) & 0xff) != bus->secondary) {
  91. buses = (buses & 0xff000000)
  92. | ((unsigned int)(bus->primary) << 0)
  93. | ((unsigned int)(bus->secondary) << 8)
  94. | ((unsigned int)(bus->subordinate) << 16);
  95. pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
  96. }
  97. return NOTIFY_OK;
  98. }
  99. static struct acpi_dock_ops acpiphp_dock_ops = {
  100. .handler = handle_hotplug_event_func,
  101. };
  102. /* callback routine to register each ACPI PCI slot object */
  103. static acpi_status
  104. register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
  105. {
  106. struct acpiphp_bridge *bridge = (struct acpiphp_bridge *)context;
  107. struct acpiphp_slot *slot;
  108. struct acpiphp_func *newfunc;
  109. acpi_handle tmp;
  110. acpi_status status = AE_OK;
  111. unsigned long long adr, sun;
  112. int device, function, retval;
  113. struct pci_bus *pbus = bridge->pci_bus;
  114. struct pci_dev *pdev;
  115. if (!acpi_pci_check_ejectable(pbus, handle) && !is_dock_device(handle))
  116. return AE_OK;
  117. acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
  118. device = (adr >> 16) & 0xffff;
  119. function = adr & 0xffff;
  120. newfunc = kzalloc(sizeof(struct acpiphp_func), GFP_KERNEL);
  121. if (!newfunc)
  122. return AE_NO_MEMORY;
  123. INIT_LIST_HEAD(&newfunc->sibling);
  124. newfunc->handle = handle;
  125. newfunc->function = function;
  126. if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
  127. newfunc->flags = FUNC_HAS_EJ0;
  128. if (ACPI_SUCCESS(acpi_get_handle(handle, "_STA", &tmp)))
  129. newfunc->flags |= FUNC_HAS_STA;
  130. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS0", &tmp)))
  131. newfunc->flags |= FUNC_HAS_PS0;
  132. if (ACPI_SUCCESS(acpi_get_handle(handle, "_PS3", &tmp)))
  133. newfunc->flags |= FUNC_HAS_PS3;
  134. if (ACPI_SUCCESS(acpi_get_handle(handle, "_DCK", &tmp)))
  135. newfunc->flags |= FUNC_HAS_DCK;
  136. status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
  137. if (ACPI_FAILURE(status)) {
  138. /*
  139. * use the count of the number of slots we've found
  140. * for the number of the slot
  141. */
  142. sun = bridge->nr_slots+1;
  143. }
  144. /* search for objects that share the same slot */
  145. for (slot = bridge->slots; slot; slot = slot->next)
  146. if (slot->device == device) {
  147. if (slot->sun != sun)
  148. warn("sibling found, but _SUN doesn't match!\n");
  149. break;
  150. }
  151. if (!slot) {
  152. slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
  153. if (!slot) {
  154. kfree(newfunc);
  155. return AE_NO_MEMORY;
  156. }
  157. slot->bridge = bridge;
  158. slot->device = device;
  159. slot->sun = sun;
  160. INIT_LIST_HEAD(&slot->funcs);
  161. mutex_init(&slot->crit_sect);
  162. slot->next = bridge->slots;
  163. bridge->slots = slot;
  164. bridge->nr_slots++;
  165. dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
  166. slot->sun, pci_domain_nr(pbus), pbus->number, device);
  167. retval = acpiphp_register_hotplug_slot(slot);
  168. if (retval) {
  169. if (retval == -EBUSY)
  170. warn("Slot %llu already registered by another "
  171. "hotplug driver\n", slot->sun);
  172. else
  173. warn("acpiphp_register_hotplug_slot failed "
  174. "(err code = 0x%x)\n", retval);
  175. goto err_exit;
  176. }
  177. }
  178. newfunc->slot = slot;
  179. list_add_tail(&newfunc->sibling, &slot->funcs);
  180. pdev = pci_get_slot(pbus, PCI_DEVFN(device, function));
  181. if (pdev) {
  182. slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
  183. pci_dev_put(pdev);
  184. }
  185. if (is_dock_device(handle)) {
  186. /* we don't want to call this device's _EJ0
  187. * because we want the dock notify handler
  188. * to call it after it calls _DCK
  189. */
  190. newfunc->flags &= ~FUNC_HAS_EJ0;
  191. if (register_hotplug_dock_device(handle,
  192. &acpiphp_dock_ops, newfunc))
  193. dbg("failed to register dock device\n");
  194. /* we need to be notified when dock events happen
  195. * outside of the hotplug operation, since we may
  196. * need to do fixups before we can hotplug.
  197. */
  198. newfunc->nb.notifier_call = post_dock_fixups;
  199. if (register_dock_notifier(&newfunc->nb))
  200. dbg("failed to register a dock notifier");
  201. }
  202. /* install notify handler */
  203. if (!(newfunc->flags & FUNC_HAS_DCK)) {
  204. status = acpi_install_notify_handler(handle,
  205. ACPI_SYSTEM_NOTIFY,
  206. handle_hotplug_event_func,
  207. newfunc);
  208. if (ACPI_FAILURE(status))
  209. err("failed to register interrupt notify handler\n");
  210. } else
  211. status = AE_OK;
  212. return status;
  213. err_exit:
  214. bridge->nr_slots--;
  215. bridge->slots = slot->next;
  216. kfree(slot);
  217. kfree(newfunc);
  218. return AE_OK;
  219. }
  220. /* see if it's worth looking at this bridge */
  221. static int detect_ejectable_slots(struct pci_bus *pbus)
  222. {
  223. int found = acpi_pci_detect_ejectable(pbus);
  224. if (!found) {
  225. acpi_handle bridge_handle = acpi_pci_get_bridge_handle(pbus);
  226. if (!bridge_handle)
  227. return 0;
  228. acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge_handle, (u32)1,
  229. is_pci_dock_device, (void *)&found, NULL);
  230. }
  231. return found;
  232. }
  233. /* decode ACPI 2.0 _HPP hot plug parameters */
  234. static void decode_hpp(struct acpiphp_bridge *bridge)
  235. {
  236. acpi_status status;
  237. status = acpi_get_hp_params_from_firmware(bridge->pci_bus, &bridge->hpp);
  238. if (ACPI_FAILURE(status) ||
  239. !bridge->hpp.t0 || (bridge->hpp.t0->revision > 1)) {
  240. /* use default numbers */
  241. printk(KERN_WARNING
  242. "%s: Could not get hotplug parameters. Use defaults\n",
  243. __func__);
  244. bridge->hpp.t0 = &bridge->hpp.type0_data;
  245. bridge->hpp.t0->revision = 0;
  246. bridge->hpp.t0->cache_line_size = 0x10;
  247. bridge->hpp.t0->latency_timer = 0x40;
  248. bridge->hpp.t0->enable_serr = 0;
  249. bridge->hpp.t0->enable_perr = 0;
  250. }
  251. }
  252. /* initialize miscellaneous stuff for both root and PCI-to-PCI bridge */
  253. static void init_bridge_misc(struct acpiphp_bridge *bridge)
  254. {
  255. acpi_status status;
  256. /* decode ACPI 2.0 _HPP (hot plug parameters) */
  257. decode_hpp(bridge);
  258. /* must be added to the list prior to calling register_slot */
  259. list_add(&bridge->list, &bridge_list);
  260. /* register all slot objects under this bridge */
  261. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, bridge->handle, (u32)1,
  262. register_slot, bridge, NULL);
  263. if (ACPI_FAILURE(status)) {
  264. list_del(&bridge->list);
  265. return;
  266. }
  267. /* install notify handler */
  268. if (bridge->type != BRIDGE_TYPE_HOST) {
  269. if ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func) {
  270. status = acpi_remove_notify_handler(bridge->func->handle,
  271. ACPI_SYSTEM_NOTIFY,
  272. handle_hotplug_event_func);
  273. if (ACPI_FAILURE(status))
  274. err("failed to remove notify handler\n");
  275. }
  276. status = acpi_install_notify_handler(bridge->handle,
  277. ACPI_SYSTEM_NOTIFY,
  278. handle_hotplug_event_bridge,
  279. bridge);
  280. if (ACPI_FAILURE(status)) {
  281. err("failed to register interrupt notify handler\n");
  282. }
  283. }
  284. }
  285. /* find acpiphp_func from acpiphp_bridge */
  286. static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle)
  287. {
  288. struct list_head *node, *l;
  289. struct acpiphp_bridge *bridge;
  290. struct acpiphp_slot *slot;
  291. struct acpiphp_func *func;
  292. list_for_each(node, &bridge_list) {
  293. bridge = list_entry(node, struct acpiphp_bridge, list);
  294. for (slot = bridge->slots; slot; slot = slot->next) {
  295. list_for_each(l, &slot->funcs) {
  296. func = list_entry(l, struct acpiphp_func,
  297. sibling);
  298. if (func->handle == handle)
  299. return func;
  300. }
  301. }
  302. }
  303. return NULL;
  304. }
  305. static inline void config_p2p_bridge_flags(struct acpiphp_bridge *bridge)
  306. {
  307. acpi_handle dummy_handle;
  308. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  309. "_STA", &dummy_handle)))
  310. bridge->flags |= BRIDGE_HAS_STA;
  311. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  312. "_EJ0", &dummy_handle)))
  313. bridge->flags |= BRIDGE_HAS_EJ0;
  314. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  315. "_PS0", &dummy_handle)))
  316. bridge->flags |= BRIDGE_HAS_PS0;
  317. if (ACPI_SUCCESS(acpi_get_handle(bridge->handle,
  318. "_PS3", &dummy_handle)))
  319. bridge->flags |= BRIDGE_HAS_PS3;
  320. /* is this ejectable p2p bridge? */
  321. if (bridge->flags & BRIDGE_HAS_EJ0) {
  322. struct acpiphp_func *func;
  323. dbg("found ejectable p2p bridge\n");
  324. /* make link between PCI bridge and PCI function */
  325. func = acpiphp_bridge_handle_to_function(bridge->handle);
  326. if (!func)
  327. return;
  328. bridge->func = func;
  329. func->bridge = bridge;
  330. }
  331. }
  332. /* allocate and initialize host bridge data structure */
  333. static void add_host_bridge(acpi_handle *handle, struct pci_bus *pci_bus)
  334. {
  335. struct acpiphp_bridge *bridge;
  336. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  337. if (bridge == NULL)
  338. return;
  339. bridge->type = BRIDGE_TYPE_HOST;
  340. bridge->handle = handle;
  341. bridge->pci_bus = pci_bus;
  342. spin_lock_init(&bridge->res_lock);
  343. init_bridge_misc(bridge);
  344. }
  345. /* allocate and initialize PCI-to-PCI bridge data structure */
  346. static void add_p2p_bridge(acpi_handle *handle, struct pci_dev *pci_dev)
  347. {
  348. struct acpiphp_bridge *bridge;
  349. bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
  350. if (bridge == NULL) {
  351. err("out of memory\n");
  352. return;
  353. }
  354. bridge->type = BRIDGE_TYPE_P2P;
  355. bridge->handle = handle;
  356. config_p2p_bridge_flags(bridge);
  357. bridge->pci_dev = pci_dev_get(pci_dev);
  358. bridge->pci_bus = pci_dev->subordinate;
  359. if (!bridge->pci_bus) {
  360. err("This is not a PCI-to-PCI bridge!\n");
  361. goto err;
  362. }
  363. /*
  364. * Grab a ref to the subordinate PCI bus in case the bus is
  365. * removed via PCI core logical hotplug. The ref pins the bus
  366. * (which we access during module unload).
  367. */
  368. get_device(&bridge->pci_bus->dev);
  369. spin_lock_init(&bridge->res_lock);
  370. init_bridge_misc(bridge);
  371. return;
  372. err:
  373. pci_dev_put(pci_dev);
  374. kfree(bridge);
  375. return;
  376. }
  377. /* callback routine to find P2P bridges */
  378. static acpi_status
  379. find_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  380. {
  381. acpi_status status;
  382. acpi_handle dummy_handle;
  383. unsigned long long tmp;
  384. int device, function;
  385. struct pci_dev *dev;
  386. struct pci_bus *pci_bus = context;
  387. status = acpi_get_handle(handle, "_ADR", &dummy_handle);
  388. if (ACPI_FAILURE(status))
  389. return AE_OK; /* continue */
  390. status = acpi_evaluate_integer(handle, "_ADR", NULL, &tmp);
  391. if (ACPI_FAILURE(status)) {
  392. dbg("%s: _ADR evaluation failure\n", __func__);
  393. return AE_OK;
  394. }
  395. device = (tmp >> 16) & 0xffff;
  396. function = tmp & 0xffff;
  397. dev = pci_get_slot(pci_bus, PCI_DEVFN(device, function));
  398. if (!dev || !dev->subordinate)
  399. goto out;
  400. /* check if this bridge has ejectable slots */
  401. if ((detect_ejectable_slots(dev->subordinate) > 0)) {
  402. dbg("found PCI-to-PCI bridge at PCI %s\n", pci_name(dev));
  403. add_p2p_bridge(handle, dev);
  404. }
  405. /* search P2P bridges under this p2p bridge */
  406. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  407. find_p2p_bridge, dev->subordinate, NULL);
  408. if (ACPI_FAILURE(status))
  409. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  410. out:
  411. pci_dev_put(dev);
  412. return AE_OK;
  413. }
  414. /* find hot-pluggable slots, and then find P2P bridge */
  415. static int add_bridge(acpi_handle handle)
  416. {
  417. acpi_status status;
  418. unsigned long long tmp;
  419. int seg, bus;
  420. acpi_handle dummy_handle;
  421. struct pci_bus *pci_bus;
  422. /* if the bridge doesn't have _STA, we assume it is always there */
  423. status = acpi_get_handle(handle, "_STA", &dummy_handle);
  424. if (ACPI_SUCCESS(status)) {
  425. status = acpi_evaluate_integer(handle, "_STA", NULL, &tmp);
  426. if (ACPI_FAILURE(status)) {
  427. dbg("%s: _STA evaluation failure\n", __func__);
  428. return 0;
  429. }
  430. if ((tmp & ACPI_STA_FUNCTIONING) == 0)
  431. /* don't register this object */
  432. return 0;
  433. }
  434. /* get PCI segment number */
  435. status = acpi_evaluate_integer(handle, "_SEG", NULL, &tmp);
  436. seg = ACPI_SUCCESS(status) ? tmp : 0;
  437. /* get PCI bus number */
  438. status = acpi_evaluate_integer(handle, "_BBN", NULL, &tmp);
  439. if (ACPI_SUCCESS(status)) {
  440. bus = tmp;
  441. } else {
  442. warn("can't get bus number, assuming 0\n");
  443. bus = 0;
  444. }
  445. pci_bus = pci_find_bus(seg, bus);
  446. if (!pci_bus) {
  447. err("Can't find bus %04x:%02x\n", seg, bus);
  448. return 0;
  449. }
  450. /* check if this bridge has ejectable slots */
  451. if (detect_ejectable_slots(pci_bus) > 0) {
  452. dbg("found PCI host-bus bridge with hot-pluggable slots\n");
  453. add_host_bridge(handle, pci_bus);
  454. }
  455. /* search P2P bridges under this host bridge */
  456. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  457. find_p2p_bridge, pci_bus, NULL);
  458. if (ACPI_FAILURE(status))
  459. warn("find_p2p_bridge failed (error code = 0x%x)\n", status);
  460. return 0;
  461. }
  462. static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
  463. {
  464. struct list_head *head;
  465. list_for_each(head, &bridge_list) {
  466. struct acpiphp_bridge *bridge = list_entry(head,
  467. struct acpiphp_bridge, list);
  468. if (bridge->handle == handle)
  469. return bridge;
  470. }
  471. return NULL;
  472. }
  473. static void cleanup_bridge(struct acpiphp_bridge *bridge)
  474. {
  475. struct list_head *list, *tmp;
  476. struct acpiphp_slot *slot;
  477. acpi_status status;
  478. acpi_handle handle = bridge->handle;
  479. status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  480. handle_hotplug_event_bridge);
  481. if (ACPI_FAILURE(status))
  482. err("failed to remove notify handler\n");
  483. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  484. ((bridge->flags & BRIDGE_HAS_EJ0) && bridge->func)) {
  485. status = acpi_install_notify_handler(bridge->func->handle,
  486. ACPI_SYSTEM_NOTIFY,
  487. handle_hotplug_event_func,
  488. bridge->func);
  489. if (ACPI_FAILURE(status))
  490. err("failed to install interrupt notify handler\n");
  491. }
  492. slot = bridge->slots;
  493. while (slot) {
  494. struct acpiphp_slot *next = slot->next;
  495. list_for_each_safe (list, tmp, &slot->funcs) {
  496. struct acpiphp_func *func;
  497. func = list_entry(list, struct acpiphp_func, sibling);
  498. if (is_dock_device(func->handle)) {
  499. unregister_hotplug_dock_device(func->handle);
  500. unregister_dock_notifier(&func->nb);
  501. }
  502. if (!(func->flags & FUNC_HAS_DCK)) {
  503. status = acpi_remove_notify_handler(func->handle,
  504. ACPI_SYSTEM_NOTIFY,
  505. handle_hotplug_event_func);
  506. if (ACPI_FAILURE(status))
  507. err("failed to remove notify handler\n");
  508. }
  509. list_del(list);
  510. kfree(func);
  511. }
  512. acpiphp_unregister_hotplug_slot(slot);
  513. list_del(&slot->funcs);
  514. kfree(slot);
  515. slot = next;
  516. }
  517. /*
  518. * Only P2P bridges have a pci_dev
  519. */
  520. if (bridge->pci_dev)
  521. put_device(&bridge->pci_bus->dev);
  522. pci_dev_put(bridge->pci_dev);
  523. list_del(&bridge->list);
  524. kfree(bridge);
  525. }
  526. static acpi_status
  527. cleanup_p2p_bridge(acpi_handle handle, u32 lvl, void *context, void **rv)
  528. {
  529. struct acpiphp_bridge *bridge;
  530. /* cleanup p2p bridges under this P2P bridge
  531. in a depth-first manner */
  532. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, (u32)1,
  533. cleanup_p2p_bridge, NULL, NULL);
  534. bridge = acpiphp_handle_to_bridge(handle);
  535. if (bridge)
  536. cleanup_bridge(bridge);
  537. return AE_OK;
  538. }
  539. static void remove_bridge(acpi_handle handle)
  540. {
  541. struct acpiphp_bridge *bridge;
  542. /* cleanup p2p bridges under this host bridge
  543. in a depth-first manner */
  544. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  545. (u32)1, cleanup_p2p_bridge, NULL, NULL);
  546. /*
  547. * On root bridges with hotplug slots directly underneath (ie,
  548. * no p2p bridge inbetween), we call cleanup_bridge().
  549. *
  550. * The else clause cleans up root bridges that either had no
  551. * hotplug slots at all, or had a p2p bridge underneath.
  552. */
  553. bridge = acpiphp_handle_to_bridge(handle);
  554. if (bridge)
  555. cleanup_bridge(bridge);
  556. else
  557. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  558. handle_hotplug_event_bridge);
  559. }
  560. static struct pci_dev * get_apic_pci_info(acpi_handle handle)
  561. {
  562. struct acpi_pci_id id;
  563. struct pci_bus *bus;
  564. struct pci_dev *dev;
  565. if (ACPI_FAILURE(acpi_get_pci_id(handle, &id)))
  566. return NULL;
  567. bus = pci_find_bus(id.segment, id.bus);
  568. if (!bus)
  569. return NULL;
  570. dev = pci_get_slot(bus, PCI_DEVFN(id.device, id.function));
  571. if (!dev)
  572. return NULL;
  573. if ((dev->class != PCI_CLASS_SYSTEM_PIC_IOAPIC) &&
  574. (dev->class != PCI_CLASS_SYSTEM_PIC_IOXAPIC))
  575. {
  576. pci_dev_put(dev);
  577. return NULL;
  578. }
  579. return dev;
  580. }
  581. static int get_gsi_base(acpi_handle handle, u32 *gsi_base)
  582. {
  583. acpi_status status;
  584. int result = -1;
  585. unsigned long long gsb;
  586. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  587. union acpi_object *obj;
  588. void *table;
  589. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsb);
  590. if (ACPI_SUCCESS(status)) {
  591. *gsi_base = (u32)gsb;
  592. return 0;
  593. }
  594. status = acpi_evaluate_object(handle, "_MAT", NULL, &buffer);
  595. if (ACPI_FAILURE(status) || !buffer.length || !buffer.pointer)
  596. return -1;
  597. obj = buffer.pointer;
  598. if (obj->type != ACPI_TYPE_BUFFER)
  599. goto out;
  600. table = obj->buffer.pointer;
  601. switch (((struct acpi_subtable_header *)table)->type) {
  602. case ACPI_MADT_TYPE_IO_SAPIC:
  603. *gsi_base = ((struct acpi_madt_io_sapic *)table)->global_irq_base;
  604. result = 0;
  605. break;
  606. case ACPI_MADT_TYPE_IO_APIC:
  607. *gsi_base = ((struct acpi_madt_io_apic *)table)->global_irq_base;
  608. result = 0;
  609. break;
  610. default:
  611. break;
  612. }
  613. out:
  614. kfree(buffer.pointer);
  615. return result;
  616. }
  617. static acpi_status
  618. ioapic_add(acpi_handle handle, u32 lvl, void *context, void **rv)
  619. {
  620. acpi_status status;
  621. unsigned long long sta;
  622. acpi_handle tmp;
  623. struct pci_dev *pdev;
  624. u32 gsi_base;
  625. u64 phys_addr;
  626. struct acpiphp_ioapic *ioapic;
  627. /* Evaluate _STA if present */
  628. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  629. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  630. return AE_CTRL_DEPTH;
  631. /* Scan only PCI bus scope */
  632. status = acpi_get_handle(handle, "_HID", &tmp);
  633. if (ACPI_SUCCESS(status))
  634. return AE_CTRL_DEPTH;
  635. if (get_gsi_base(handle, &gsi_base))
  636. return AE_OK;
  637. ioapic = kmalloc(sizeof(*ioapic), GFP_KERNEL);
  638. if (!ioapic)
  639. return AE_NO_MEMORY;
  640. pdev = get_apic_pci_info(handle);
  641. if (!pdev)
  642. goto exit_kfree;
  643. if (pci_enable_device(pdev))
  644. goto exit_pci_dev_put;
  645. pci_set_master(pdev);
  646. if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)"))
  647. goto exit_pci_disable_device;
  648. phys_addr = pci_resource_start(pdev, 0);
  649. if (acpi_register_ioapic(handle, phys_addr, gsi_base))
  650. goto exit_pci_release_region;
  651. ioapic->gsi_base = gsi_base;
  652. ioapic->dev = pdev;
  653. spin_lock(&ioapic_list_lock);
  654. list_add_tail(&ioapic->list, &ioapic_list);
  655. spin_unlock(&ioapic_list_lock);
  656. return AE_OK;
  657. exit_pci_release_region:
  658. pci_release_region(pdev, 0);
  659. exit_pci_disable_device:
  660. pci_disable_device(pdev);
  661. exit_pci_dev_put:
  662. pci_dev_put(pdev);
  663. exit_kfree:
  664. kfree(ioapic);
  665. return AE_OK;
  666. }
  667. static acpi_status
  668. ioapic_remove(acpi_handle handle, u32 lvl, void *context, void **rv)
  669. {
  670. acpi_status status;
  671. unsigned long long sta;
  672. acpi_handle tmp;
  673. u32 gsi_base;
  674. struct acpiphp_ioapic *pos, *n, *ioapic = NULL;
  675. /* Evaluate _STA if present */
  676. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  677. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  678. return AE_CTRL_DEPTH;
  679. /* Scan only PCI bus scope */
  680. status = acpi_get_handle(handle, "_HID", &tmp);
  681. if (ACPI_SUCCESS(status))
  682. return AE_CTRL_DEPTH;
  683. if (get_gsi_base(handle, &gsi_base))
  684. return AE_OK;
  685. acpi_unregister_ioapic(handle, gsi_base);
  686. spin_lock(&ioapic_list_lock);
  687. list_for_each_entry_safe(pos, n, &ioapic_list, list) {
  688. if (pos->gsi_base != gsi_base)
  689. continue;
  690. ioapic = pos;
  691. list_del(&ioapic->list);
  692. break;
  693. }
  694. spin_unlock(&ioapic_list_lock);
  695. if (!ioapic)
  696. return AE_OK;
  697. pci_release_region(ioapic->dev, 0);
  698. pci_disable_device(ioapic->dev);
  699. pci_dev_put(ioapic->dev);
  700. kfree(ioapic);
  701. return AE_OK;
  702. }
  703. static int acpiphp_configure_ioapics(acpi_handle handle)
  704. {
  705. ioapic_add(handle, 0, NULL, NULL);
  706. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  707. ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
  708. return 0;
  709. }
  710. static int acpiphp_unconfigure_ioapics(acpi_handle handle)
  711. {
  712. ioapic_remove(handle, 0, NULL, NULL);
  713. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  714. ACPI_UINT32_MAX, ioapic_remove, NULL, NULL);
  715. return 0;
  716. }
  717. static int power_on_slot(struct acpiphp_slot *slot)
  718. {
  719. acpi_status status;
  720. struct acpiphp_func *func;
  721. struct list_head *l;
  722. int retval = 0;
  723. /* if already enabled, just skip */
  724. if (slot->flags & SLOT_POWEREDON)
  725. goto err_exit;
  726. list_for_each (l, &slot->funcs) {
  727. func = list_entry(l, struct acpiphp_func, sibling);
  728. if (func->flags & FUNC_HAS_PS0) {
  729. dbg("%s: executing _PS0\n", __func__);
  730. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  731. if (ACPI_FAILURE(status)) {
  732. warn("%s: _PS0 failed\n", __func__);
  733. retval = -1;
  734. goto err_exit;
  735. } else
  736. break;
  737. }
  738. }
  739. /* TBD: evaluate _STA to check if the slot is enabled */
  740. slot->flags |= SLOT_POWEREDON;
  741. err_exit:
  742. return retval;
  743. }
  744. static int power_off_slot(struct acpiphp_slot *slot)
  745. {
  746. acpi_status status;
  747. struct acpiphp_func *func;
  748. struct list_head *l;
  749. int retval = 0;
  750. /* if already disabled, just skip */
  751. if ((slot->flags & SLOT_POWEREDON) == 0)
  752. goto err_exit;
  753. list_for_each (l, &slot->funcs) {
  754. func = list_entry(l, struct acpiphp_func, sibling);
  755. if (func->flags & FUNC_HAS_PS3) {
  756. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  757. if (ACPI_FAILURE(status)) {
  758. warn("%s: _PS3 failed\n", __func__);
  759. retval = -1;
  760. goto err_exit;
  761. } else
  762. break;
  763. }
  764. }
  765. /* TBD: evaluate _STA to check if the slot is disabled */
  766. slot->flags &= (~SLOT_POWEREDON);
  767. err_exit:
  768. return retval;
  769. }
  770. /**
  771. * acpiphp_max_busnr - return the highest reserved bus number under the given bus.
  772. * @bus: bus to start search with
  773. */
  774. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  775. {
  776. struct list_head *tmp;
  777. unsigned char max, n;
  778. /*
  779. * pci_bus_max_busnr will return the highest
  780. * reserved busnr for all these children.
  781. * that is equivalent to the bus->subordinate
  782. * value. We don't want to use the parent's
  783. * bus->subordinate value because it could have
  784. * padding in it.
  785. */
  786. max = bus->secondary;
  787. list_for_each(tmp, &bus->children) {
  788. n = pci_bus_max_busnr(pci_bus_b(tmp));
  789. if (n > max)
  790. max = n;
  791. }
  792. return max;
  793. }
  794. /**
  795. * acpiphp_bus_add - add a new bus to acpi subsystem
  796. * @func: acpiphp_func of the bridge
  797. */
  798. static int acpiphp_bus_add(struct acpiphp_func *func)
  799. {
  800. acpi_handle phandle;
  801. struct acpi_device *device, *pdevice;
  802. int ret_val;
  803. acpi_get_parent(func->handle, &phandle);
  804. if (acpi_bus_get_device(phandle, &pdevice)) {
  805. dbg("no parent device, assuming NULL\n");
  806. pdevice = NULL;
  807. }
  808. if (!acpi_bus_get_device(func->handle, &device)) {
  809. dbg("bus exists... trim\n");
  810. /* this shouldn't be in here, so remove
  811. * the bus then re-add it...
  812. */
  813. ret_val = acpi_bus_trim(device, 1);
  814. dbg("acpi_bus_trim return %x\n", ret_val);
  815. }
  816. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  817. ACPI_BUS_TYPE_DEVICE);
  818. if (ret_val) {
  819. dbg("error adding bus, %x\n",
  820. -ret_val);
  821. goto acpiphp_bus_add_out;
  822. }
  823. /*
  824. * try to start anyway. We could have failed to add
  825. * simply because this bus had previously been added
  826. * on another add. Don't bother with the return value
  827. * we just keep going.
  828. */
  829. ret_val = acpi_bus_start(device);
  830. acpiphp_bus_add_out:
  831. return ret_val;
  832. }
  833. /**
  834. * acpiphp_bus_trim - trim a bus from acpi subsystem
  835. * @handle: handle to acpi namespace
  836. */
  837. static int acpiphp_bus_trim(acpi_handle handle)
  838. {
  839. struct acpi_device *device;
  840. int retval;
  841. retval = acpi_bus_get_device(handle, &device);
  842. if (retval) {
  843. dbg("acpi_device not found\n");
  844. return retval;
  845. }
  846. retval = acpi_bus_trim(device, 1);
  847. if (retval)
  848. err("cannot remove from acpi list\n");
  849. return retval;
  850. }
  851. /**
  852. * enable_device - enable, configure a slot
  853. * @slot: slot to be enabled
  854. *
  855. * This function should be called per *physical slot*,
  856. * not per each slot object in ACPI namespace.
  857. */
  858. static int __ref enable_device(struct acpiphp_slot *slot)
  859. {
  860. struct pci_dev *dev;
  861. struct pci_bus *bus = slot->bridge->pci_bus;
  862. struct list_head *l;
  863. struct acpiphp_func *func;
  864. int retval = 0;
  865. int num, max, pass;
  866. acpi_status status;
  867. if (slot->flags & SLOT_ENABLED)
  868. goto err_exit;
  869. /* sanity check: dev should be NULL when hot-plugged in */
  870. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  871. if (dev) {
  872. /* This case shouldn't happen */
  873. err("pci_dev structure already exists.\n");
  874. pci_dev_put(dev);
  875. retval = -1;
  876. goto err_exit;
  877. }
  878. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  879. if (num == 0) {
  880. err("No new device found\n");
  881. retval = -1;
  882. goto err_exit;
  883. }
  884. max = acpiphp_max_busnr(bus);
  885. for (pass = 0; pass < 2; pass++) {
  886. list_for_each_entry(dev, &bus->devices, bus_list) {
  887. if (PCI_SLOT(dev->devfn) != slot->device)
  888. continue;
  889. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  890. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  891. max = pci_scan_bridge(bus, dev, max, pass);
  892. if (pass && dev->subordinate)
  893. pci_bus_size_bridges(dev->subordinate);
  894. }
  895. }
  896. }
  897. list_for_each (l, &slot->funcs) {
  898. func = list_entry(l, struct acpiphp_func, sibling);
  899. acpiphp_bus_add(func);
  900. }
  901. pci_bus_assign_resources(bus);
  902. acpiphp_sanitize_bus(bus);
  903. acpiphp_set_hpp_values(slot->bridge->handle, bus);
  904. list_for_each_entry(func, &slot->funcs, sibling)
  905. acpiphp_configure_ioapics(func->handle);
  906. pci_enable_bridges(bus);
  907. pci_bus_add_devices(bus);
  908. list_for_each (l, &slot->funcs) {
  909. func = list_entry(l, struct acpiphp_func, sibling);
  910. dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  911. func->function));
  912. if (!dev)
  913. continue;
  914. if (dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  915. dev->hdr_type != PCI_HEADER_TYPE_CARDBUS) {
  916. pci_dev_put(dev);
  917. continue;
  918. }
  919. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  920. if (ACPI_FAILURE(status))
  921. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  922. status);
  923. pci_dev_put(dev);
  924. }
  925. slot->flags |= SLOT_ENABLED;
  926. err_exit:
  927. return retval;
  928. }
  929. static void disable_bridges(struct pci_bus *bus)
  930. {
  931. struct pci_dev *dev;
  932. list_for_each_entry(dev, &bus->devices, bus_list) {
  933. if (dev->subordinate) {
  934. disable_bridges(dev->subordinate);
  935. pci_disable_device(dev);
  936. }
  937. }
  938. }
  939. /**
  940. * disable_device - disable a slot
  941. * @slot: ACPI PHP slot
  942. */
  943. static int disable_device(struct acpiphp_slot *slot)
  944. {
  945. struct acpiphp_func *func;
  946. struct pci_dev *pdev;
  947. /* is this slot already disabled? */
  948. if (!(slot->flags & SLOT_ENABLED))
  949. goto err_exit;
  950. list_for_each_entry(func, &slot->funcs, sibling) {
  951. if (func->bridge) {
  952. /* cleanup p2p bridges under this P2P bridge */
  953. cleanup_p2p_bridge(func->bridge->handle,
  954. (u32)1, NULL, NULL);
  955. func->bridge = NULL;
  956. }
  957. pdev = pci_get_slot(slot->bridge->pci_bus,
  958. PCI_DEVFN(slot->device, func->function));
  959. if (pdev) {
  960. pci_stop_bus_device(pdev);
  961. if (pdev->subordinate) {
  962. disable_bridges(pdev->subordinate);
  963. pci_disable_device(pdev);
  964. }
  965. pci_remove_bus_device(pdev);
  966. pci_dev_put(pdev);
  967. }
  968. }
  969. list_for_each_entry(func, &slot->funcs, sibling) {
  970. acpiphp_unconfigure_ioapics(func->handle);
  971. acpiphp_bus_trim(func->handle);
  972. }
  973. slot->flags &= (~SLOT_ENABLED);
  974. err_exit:
  975. return 0;
  976. }
  977. /**
  978. * get_slot_status - get ACPI slot status
  979. * @slot: ACPI PHP slot
  980. *
  981. * If a slot has _STA for each function and if any one of them
  982. * returned non-zero status, return it.
  983. *
  984. * If a slot doesn't have _STA and if any one of its functions'
  985. * configuration space is configured, return 0x0f as a _STA.
  986. *
  987. * Otherwise return 0.
  988. */
  989. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  990. {
  991. acpi_status status;
  992. unsigned long long sta = 0;
  993. u32 dvid;
  994. struct list_head *l;
  995. struct acpiphp_func *func;
  996. list_for_each (l, &slot->funcs) {
  997. func = list_entry(l, struct acpiphp_func, sibling);
  998. if (func->flags & FUNC_HAS_STA) {
  999. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  1000. if (ACPI_SUCCESS(status) && sta)
  1001. break;
  1002. } else {
  1003. pci_bus_read_config_dword(slot->bridge->pci_bus,
  1004. PCI_DEVFN(slot->device,
  1005. func->function),
  1006. PCI_VENDOR_ID, &dvid);
  1007. if (dvid != 0xffffffff) {
  1008. sta = ACPI_STA_ALL;
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. return (unsigned int)sta;
  1014. }
  1015. /**
  1016. * acpiphp_eject_slot - physically eject the slot
  1017. * @slot: ACPI PHP slot
  1018. */
  1019. int acpiphp_eject_slot(struct acpiphp_slot *slot)
  1020. {
  1021. acpi_status status;
  1022. struct acpiphp_func *func;
  1023. struct list_head *l;
  1024. struct acpi_object_list arg_list;
  1025. union acpi_object arg;
  1026. list_for_each (l, &slot->funcs) {
  1027. func = list_entry(l, struct acpiphp_func, sibling);
  1028. /* We don't want to call _EJ0 on non-existing functions. */
  1029. if ((func->flags & FUNC_HAS_EJ0)) {
  1030. /* _EJ0 method take one argument */
  1031. arg_list.count = 1;
  1032. arg_list.pointer = &arg;
  1033. arg.type = ACPI_TYPE_INTEGER;
  1034. arg.integer.value = 1;
  1035. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  1036. if (ACPI_FAILURE(status)) {
  1037. warn("%s: _EJ0 failed\n", __func__);
  1038. return -1;
  1039. } else
  1040. break;
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. /**
  1046. * acpiphp_check_bridge - re-enumerate devices
  1047. * @bridge: where to begin re-enumeration
  1048. *
  1049. * Iterate over all slots under this bridge and make sure that if a
  1050. * card is present they are enabled, and if not they are disabled.
  1051. */
  1052. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  1053. {
  1054. struct acpiphp_slot *slot;
  1055. int retval = 0;
  1056. int enabled, disabled;
  1057. enabled = disabled = 0;
  1058. for (slot = bridge->slots; slot; slot = slot->next) {
  1059. unsigned int status = get_slot_status(slot);
  1060. if (slot->flags & SLOT_ENABLED) {
  1061. if (status == ACPI_STA_ALL)
  1062. continue;
  1063. retval = acpiphp_disable_slot(slot);
  1064. if (retval) {
  1065. err("Error occurred in disabling\n");
  1066. goto err_exit;
  1067. } else {
  1068. acpiphp_eject_slot(slot);
  1069. }
  1070. disabled++;
  1071. } else {
  1072. if (status != ACPI_STA_ALL)
  1073. continue;
  1074. retval = acpiphp_enable_slot(slot);
  1075. if (retval) {
  1076. err("Error occurred in enabling\n");
  1077. goto err_exit;
  1078. }
  1079. enabled++;
  1080. }
  1081. }
  1082. dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
  1083. err_exit:
  1084. return retval;
  1085. }
  1086. static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
  1087. {
  1088. u16 pci_cmd, pci_bctl;
  1089. struct pci_dev *cdev;
  1090. /* Program hpp values for this device */
  1091. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  1092. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  1093. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  1094. return;
  1095. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  1096. return;
  1097. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  1098. bridge->hpp.t0->cache_line_size);
  1099. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  1100. bridge->hpp.t0->latency_timer);
  1101. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1102. if (bridge->hpp.t0->enable_serr)
  1103. pci_cmd |= PCI_COMMAND_SERR;
  1104. else
  1105. pci_cmd &= ~PCI_COMMAND_SERR;
  1106. if (bridge->hpp.t0->enable_perr)
  1107. pci_cmd |= PCI_COMMAND_PARITY;
  1108. else
  1109. pci_cmd &= ~PCI_COMMAND_PARITY;
  1110. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1111. /* Program bridge control value and child devices */
  1112. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1113. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1114. bridge->hpp.t0->latency_timer);
  1115. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1116. if (bridge->hpp.t0->enable_serr)
  1117. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1118. else
  1119. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  1120. if (bridge->hpp.t0->enable_perr)
  1121. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1122. else
  1123. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  1124. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1125. if (dev->subordinate) {
  1126. list_for_each_entry(cdev, &dev->subordinate->devices,
  1127. bus_list)
  1128. program_hpp(cdev, bridge);
  1129. }
  1130. }
  1131. }
  1132. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
  1133. {
  1134. struct acpiphp_bridge bridge;
  1135. struct pci_dev *dev;
  1136. memset(&bridge, 0, sizeof(bridge));
  1137. bridge.handle = handle;
  1138. bridge.pci_bus = bus;
  1139. bridge.pci_dev = bus->self;
  1140. decode_hpp(&bridge);
  1141. list_for_each_entry(dev, &bus->devices, bus_list)
  1142. program_hpp(dev, &bridge);
  1143. }
  1144. /*
  1145. * Remove devices for which we could not assign resources, call
  1146. * arch specific code to fix-up the bus
  1147. */
  1148. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  1149. {
  1150. struct pci_dev *dev;
  1151. int i;
  1152. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  1153. list_for_each_entry(dev, &bus->devices, bus_list) {
  1154. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  1155. struct resource *res = &dev->resource[i];
  1156. if ((res->flags & type_mask) && !res->start &&
  1157. res->end) {
  1158. /* Could not assign a required resources
  1159. * for this device, remove it */
  1160. pci_remove_bus_device(dev);
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. /* Program resources in newly inserted bridge */
  1167. static int acpiphp_configure_bridge (acpi_handle handle)
  1168. {
  1169. struct acpi_pci_id pci_id;
  1170. struct pci_bus *bus;
  1171. if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
  1172. err("cannot get PCI domain and bus number for bridge\n");
  1173. return -EINVAL;
  1174. }
  1175. bus = pci_find_bus(pci_id.segment, pci_id.bus);
  1176. if (!bus) {
  1177. err("cannot find bus %d:%d\n",
  1178. pci_id.segment, pci_id.bus);
  1179. return -EINVAL;
  1180. }
  1181. pci_bus_size_bridges(bus);
  1182. pci_bus_assign_resources(bus);
  1183. acpiphp_sanitize_bus(bus);
  1184. acpiphp_set_hpp_values(handle, bus);
  1185. pci_enable_bridges(bus);
  1186. acpiphp_configure_ioapics(handle);
  1187. return 0;
  1188. }
  1189. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  1190. {
  1191. struct acpi_device *device, *pdevice;
  1192. acpi_handle phandle;
  1193. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  1194. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  1195. err("unexpected notification type %d\n", type);
  1196. return;
  1197. }
  1198. acpi_get_parent(handle, &phandle);
  1199. if (acpi_bus_get_device(phandle, &pdevice)) {
  1200. dbg("no parent device, assuming NULL\n");
  1201. pdevice = NULL;
  1202. }
  1203. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  1204. err("cannot add bridge to acpi list\n");
  1205. return;
  1206. }
  1207. if (!acpiphp_configure_bridge(handle) &&
  1208. !acpi_bus_start(device))
  1209. add_bridge(handle);
  1210. else
  1211. err("cannot configure and start bridge\n");
  1212. }
  1213. /*
  1214. * ACPI event handlers
  1215. */
  1216. static acpi_status
  1217. count_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1218. {
  1219. int *count = (int *)context;
  1220. struct acpiphp_bridge *bridge;
  1221. bridge = acpiphp_handle_to_bridge(handle);
  1222. if (bridge)
  1223. (*count)++;
  1224. return AE_OK ;
  1225. }
  1226. static acpi_status
  1227. check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1228. {
  1229. struct acpiphp_bridge *bridge;
  1230. char objname[64];
  1231. struct acpi_buffer buffer = { .length = sizeof(objname),
  1232. .pointer = objname };
  1233. bridge = acpiphp_handle_to_bridge(handle);
  1234. if (bridge) {
  1235. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1236. dbg("%s: re-enumerating slots under %s\n",
  1237. __func__, objname);
  1238. acpiphp_check_bridge(bridge);
  1239. }
  1240. return AE_OK ;
  1241. }
  1242. /**
  1243. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1244. * @handle: Notify()'ed acpi_handle
  1245. * @type: Notify code
  1246. * @context: pointer to acpiphp_bridge structure
  1247. *
  1248. * Handles ACPI event notification on {host,p2p} bridges.
  1249. */
  1250. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  1251. {
  1252. struct acpiphp_bridge *bridge;
  1253. char objname[64];
  1254. struct acpi_buffer buffer = { .length = sizeof(objname),
  1255. .pointer = objname };
  1256. struct acpi_device *device;
  1257. int num_sub_bridges = 0;
  1258. if (acpi_bus_get_device(handle, &device)) {
  1259. /* This bridge must have just been physically inserted */
  1260. handle_bridge_insertion(handle, type);
  1261. return;
  1262. }
  1263. bridge = acpiphp_handle_to_bridge(handle);
  1264. if (type == ACPI_NOTIFY_BUS_CHECK) {
  1265. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, ACPI_UINT32_MAX,
  1266. count_sub_bridges, &num_sub_bridges, NULL);
  1267. }
  1268. if (!bridge && !num_sub_bridges) {
  1269. err("cannot get bridge info\n");
  1270. return;
  1271. }
  1272. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1273. switch (type) {
  1274. case ACPI_NOTIFY_BUS_CHECK:
  1275. /* bus re-enumerate */
  1276. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1277. if (bridge) {
  1278. dbg("%s: re-enumerating slots under %s\n",
  1279. __func__, objname);
  1280. acpiphp_check_bridge(bridge);
  1281. }
  1282. if (num_sub_bridges)
  1283. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  1284. ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL);
  1285. break;
  1286. case ACPI_NOTIFY_DEVICE_CHECK:
  1287. /* device check */
  1288. dbg("%s: Device check notify on %s\n", __func__, objname);
  1289. acpiphp_check_bridge(bridge);
  1290. break;
  1291. case ACPI_NOTIFY_DEVICE_WAKE:
  1292. /* wake event */
  1293. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1294. break;
  1295. case ACPI_NOTIFY_EJECT_REQUEST:
  1296. /* request device eject */
  1297. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1298. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1299. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1300. struct acpiphp_slot *slot;
  1301. slot = bridge->func->slot;
  1302. if (!acpiphp_disable_slot(slot))
  1303. acpiphp_eject_slot(slot);
  1304. }
  1305. break;
  1306. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1307. printk(KERN_ERR "Device %s cannot be configured due"
  1308. " to a frequency mismatch\n", objname);
  1309. break;
  1310. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1311. printk(KERN_ERR "Device %s cannot be configured due"
  1312. " to a bus mode mismatch\n", objname);
  1313. break;
  1314. case ACPI_NOTIFY_POWER_FAULT:
  1315. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1316. objname);
  1317. break;
  1318. default:
  1319. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1320. break;
  1321. }
  1322. }
  1323. /**
  1324. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1325. * @handle: Notify()'ed acpi_handle
  1326. * @type: Notify code
  1327. * @context: pointer to acpiphp_func structure
  1328. *
  1329. * Handles ACPI event notification on slots.
  1330. */
  1331. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1332. {
  1333. struct acpiphp_func *func;
  1334. char objname[64];
  1335. struct acpi_buffer buffer = { .length = sizeof(objname),
  1336. .pointer = objname };
  1337. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1338. func = (struct acpiphp_func *)context;
  1339. switch (type) {
  1340. case ACPI_NOTIFY_BUS_CHECK:
  1341. /* bus re-enumerate */
  1342. dbg("%s: Bus check notify on %s\n", __func__, objname);
  1343. acpiphp_enable_slot(func->slot);
  1344. break;
  1345. case ACPI_NOTIFY_DEVICE_CHECK:
  1346. /* device check : re-enumerate from parent bus */
  1347. dbg("%s: Device check notify on %s\n", __func__, objname);
  1348. acpiphp_check_bridge(func->slot->bridge);
  1349. break;
  1350. case ACPI_NOTIFY_DEVICE_WAKE:
  1351. /* wake event */
  1352. dbg("%s: Device wake notify on %s\n", __func__, objname);
  1353. break;
  1354. case ACPI_NOTIFY_EJECT_REQUEST:
  1355. /* request device eject */
  1356. dbg("%s: Device eject notify on %s\n", __func__, objname);
  1357. if (!(acpiphp_disable_slot(func->slot)))
  1358. acpiphp_eject_slot(func->slot);
  1359. break;
  1360. default:
  1361. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1362. break;
  1363. }
  1364. }
  1365. static acpi_status
  1366. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1367. {
  1368. int *count = (int *)context;
  1369. if (acpi_root_bridge(handle)) {
  1370. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1371. handle_hotplug_event_bridge, NULL);
  1372. (*count)++;
  1373. }
  1374. return AE_OK ;
  1375. }
  1376. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1377. .add = add_bridge,
  1378. .remove = remove_bridge,
  1379. };
  1380. /**
  1381. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1382. */
  1383. int __init acpiphp_glue_init(void)
  1384. {
  1385. int num = 0;
  1386. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1387. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1388. if (num <= 0)
  1389. return -1;
  1390. else
  1391. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1392. return 0;
  1393. }
  1394. /**
  1395. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1396. *
  1397. * This function frees all data allocated in acpiphp_glue_init().
  1398. */
  1399. void acpiphp_glue_exit(void)
  1400. {
  1401. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1402. }
  1403. /**
  1404. * acpiphp_get_num_slots - count number of slots in a system
  1405. */
  1406. int __init acpiphp_get_num_slots(void)
  1407. {
  1408. struct acpiphp_bridge *bridge;
  1409. int num_slots = 0;
  1410. list_for_each_entry (bridge, &bridge_list, list) {
  1411. dbg("Bus %04x:%02x has %d slot%s\n",
  1412. pci_domain_nr(bridge->pci_bus),
  1413. bridge->pci_bus->number, bridge->nr_slots,
  1414. bridge->nr_slots == 1 ? "" : "s");
  1415. num_slots += bridge->nr_slots;
  1416. }
  1417. dbg("Total %d slots\n", num_slots);
  1418. return num_slots;
  1419. }
  1420. #if 0
  1421. /**
  1422. * acpiphp_for_each_slot - call function for each slot
  1423. * @fn: callback function
  1424. * @data: context to be passed to callback function
  1425. */
  1426. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1427. {
  1428. struct list_head *node;
  1429. struct acpiphp_bridge *bridge;
  1430. struct acpiphp_slot *slot;
  1431. int retval = 0;
  1432. list_for_each (node, &bridge_list) {
  1433. bridge = (struct acpiphp_bridge *)node;
  1434. for (slot = bridge->slots; slot; slot = slot->next) {
  1435. retval = fn(slot, data);
  1436. if (!retval)
  1437. goto err_exit;
  1438. }
  1439. }
  1440. err_exit:
  1441. return retval;
  1442. }
  1443. #endif
  1444. /**
  1445. * acpiphp_enable_slot - power on slot
  1446. * @slot: ACPI PHP slot
  1447. */
  1448. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1449. {
  1450. int retval;
  1451. mutex_lock(&slot->crit_sect);
  1452. /* wake up all functions */
  1453. retval = power_on_slot(slot);
  1454. if (retval)
  1455. goto err_exit;
  1456. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1457. /* configure all functions */
  1458. retval = enable_device(slot);
  1459. if (retval)
  1460. power_off_slot(slot);
  1461. } else {
  1462. dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
  1463. power_off_slot(slot);
  1464. }
  1465. err_exit:
  1466. mutex_unlock(&slot->crit_sect);
  1467. return retval;
  1468. }
  1469. /**
  1470. * acpiphp_disable_slot - power off slot
  1471. * @slot: ACPI PHP slot
  1472. */
  1473. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1474. {
  1475. int retval = 0;
  1476. mutex_lock(&slot->crit_sect);
  1477. /* unconfigure all functions */
  1478. retval = disable_device(slot);
  1479. if (retval)
  1480. goto err_exit;
  1481. /* power off all functions */
  1482. retval = power_off_slot(slot);
  1483. if (retval)
  1484. goto err_exit;
  1485. err_exit:
  1486. mutex_unlock(&slot->crit_sect);
  1487. return retval;
  1488. }
  1489. /*
  1490. * slot enabled: 1
  1491. * slot disabled: 0
  1492. */
  1493. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1494. {
  1495. return (slot->flags & SLOT_POWEREDON);
  1496. }
  1497. /*
  1498. * latch open: 1
  1499. * latch closed: 0
  1500. */
  1501. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1502. {
  1503. unsigned int sta;
  1504. sta = get_slot_status(slot);
  1505. return (sta & ACPI_STA_SHOW_IN_UI) ? 0 : 1;
  1506. }
  1507. /*
  1508. * adapter presence : 1
  1509. * absence : 0
  1510. */
  1511. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1512. {
  1513. unsigned int sta;
  1514. sta = get_slot_status(slot);
  1515. return (sta == 0) ? 0 : 1;
  1516. }