acpiphp_glue.c 44 KB

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