acpiphp_glue.c 44 KB

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