acpiphp_glue.c 44 KB

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