acpiphp_glue.c 45 KB

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