acpiphp_glue.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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/smp_lock.h>
  46. #include <linux/mutex.h>
  47. #include "../pci.h"
  48. #include "pci_hotplug.h"
  49. #include "acpiphp.h"
  50. static LIST_HEAD(bridge_list);
  51. #define MY_NAME "acpiphp_glue"
  52. static void handle_hotplug_event_bridge (acpi_handle, u32, void *);
  53. static void acpiphp_sanitize_bus(struct pci_bus *bus);
  54. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus);
  55. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context);
  56. /*
  57. * initialization & terminatation routines
  58. */
  59. /**
  60. * is_ejectable - determine if a slot is ejectable
  61. * @handle: handle to acpi namespace
  62. *
  63. * Ejectable slot should satisfy at least these conditions:
  64. *
  65. * 1. has _ADR method
  66. * 2. has _EJ0 method
  67. *
  68. * optionally
  69. *
  70. * 1. has _STA method
  71. * 2. has _PS0 method
  72. * 3. has _PS3 method
  73. * 4. ..
  74. *
  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 (((acpi_table_entry_header *)table)->type) {
  629. case ACPI_MADT_IOSAPIC:
  630. *gsi_base = ((struct acpi_table_iosapic *)table)->global_irq_base;
  631. result = 0;
  632. break;
  633. case ACPI_MADT_IOAPIC:
  634. *gsi_base = ((struct acpi_table_ioapic *)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. /* Evaluate _STA if present */
  654. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  655. if (ACPI_SUCCESS(status) && sta != ACPI_STA_ALL)
  656. return AE_CTRL_DEPTH;
  657. /* Scan only PCI bus scope */
  658. status = acpi_get_handle(handle, "_HID", &tmp);
  659. if (ACPI_SUCCESS(status))
  660. return AE_CTRL_DEPTH;
  661. if (get_gsi_base(handle, &gsi_base))
  662. return AE_OK;
  663. pdev = get_apic_pci_info(handle);
  664. if (!pdev)
  665. return AE_OK;
  666. if (pci_enable_device(pdev)) {
  667. pci_dev_put(pdev);
  668. return AE_OK;
  669. }
  670. pci_set_master(pdev);
  671. if (pci_request_region(pdev, 0, "I/O APIC(acpiphp)")) {
  672. pci_disable_device(pdev);
  673. pci_dev_put(pdev);
  674. return AE_OK;
  675. }
  676. phys_addr = pci_resource_start(pdev, 0);
  677. if (acpi_register_ioapic(handle, phys_addr, gsi_base)) {
  678. pci_release_region(pdev, 0);
  679. pci_disable_device(pdev);
  680. pci_dev_put(pdev);
  681. return AE_OK;
  682. }
  683. return AE_OK;
  684. }
  685. static int acpiphp_configure_ioapics(acpi_handle handle)
  686. {
  687. acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
  688. ACPI_UINT32_MAX, ioapic_add, NULL, NULL);
  689. return 0;
  690. }
  691. static int power_on_slot(struct acpiphp_slot *slot)
  692. {
  693. acpi_status status;
  694. struct acpiphp_func *func;
  695. struct list_head *l;
  696. int retval = 0;
  697. /* if already enabled, just skip */
  698. if (slot->flags & SLOT_POWEREDON)
  699. goto err_exit;
  700. list_for_each (l, &slot->funcs) {
  701. func = list_entry(l, struct acpiphp_func, sibling);
  702. if (func->flags & FUNC_HAS_PS0) {
  703. dbg("%s: executing _PS0\n", __FUNCTION__);
  704. status = acpi_evaluate_object(func->handle, "_PS0", NULL, NULL);
  705. if (ACPI_FAILURE(status)) {
  706. warn("%s: _PS0 failed\n", __FUNCTION__);
  707. retval = -1;
  708. goto err_exit;
  709. } else
  710. break;
  711. }
  712. }
  713. /* TBD: evaluate _STA to check if the slot is enabled */
  714. slot->flags |= SLOT_POWEREDON;
  715. err_exit:
  716. return retval;
  717. }
  718. static int power_off_slot(struct acpiphp_slot *slot)
  719. {
  720. acpi_status status;
  721. struct acpiphp_func *func;
  722. struct list_head *l;
  723. int retval = 0;
  724. /* if already disabled, just skip */
  725. if ((slot->flags & SLOT_POWEREDON) == 0)
  726. goto err_exit;
  727. list_for_each (l, &slot->funcs) {
  728. func = list_entry(l, struct acpiphp_func, sibling);
  729. if (func->flags & FUNC_HAS_PS3) {
  730. status = acpi_evaluate_object(func->handle, "_PS3", NULL, NULL);
  731. if (ACPI_FAILURE(status)) {
  732. warn("%s: _PS3 failed\n", __FUNCTION__);
  733. retval = -1;
  734. goto err_exit;
  735. } else
  736. break;
  737. }
  738. }
  739. /* TBD: evaluate _STA to check if the slot is disabled */
  740. slot->flags &= (~SLOT_POWEREDON);
  741. err_exit:
  742. return retval;
  743. }
  744. /**
  745. * acpiphp_max_busnr - return the highest reserved bus number under
  746. * the given bus.
  747. * @bus: bus to start search with
  748. *
  749. */
  750. static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
  751. {
  752. struct list_head *tmp;
  753. unsigned char max, n;
  754. /*
  755. * pci_bus_max_busnr will return the highest
  756. * reserved busnr for all these children.
  757. * that is equivalent to the bus->subordinate
  758. * value. We don't want to use the parent's
  759. * bus->subordinate value because it could have
  760. * padding in it.
  761. */
  762. max = bus->secondary;
  763. list_for_each(tmp, &bus->children) {
  764. n = pci_bus_max_busnr(pci_bus_b(tmp));
  765. if (n > max)
  766. max = n;
  767. }
  768. return max;
  769. }
  770. /**
  771. * acpiphp_bus_add - add a new bus to acpi subsystem
  772. * @func: acpiphp_func of the bridge
  773. *
  774. */
  775. static int acpiphp_bus_add(struct acpiphp_func *func)
  776. {
  777. acpi_handle phandle;
  778. struct acpi_device *device, *pdevice;
  779. int ret_val;
  780. acpi_get_parent(func->handle, &phandle);
  781. if (acpi_bus_get_device(phandle, &pdevice)) {
  782. dbg("no parent device, assuming NULL\n");
  783. pdevice = NULL;
  784. }
  785. if (!acpi_bus_get_device(func->handle, &device)) {
  786. dbg("bus exists... trim\n");
  787. /* this shouldn't be in here, so remove
  788. * the bus then re-add it...
  789. */
  790. ret_val = acpi_bus_trim(device, 1);
  791. dbg("acpi_bus_trim return %x\n", ret_val);
  792. }
  793. ret_val = acpi_bus_add(&device, pdevice, func->handle,
  794. ACPI_BUS_TYPE_DEVICE);
  795. if (ret_val) {
  796. dbg("error adding bus, %x\n",
  797. -ret_val);
  798. goto acpiphp_bus_add_out;
  799. }
  800. /*
  801. * try to start anyway. We could have failed to add
  802. * simply because this bus had previously been added
  803. * on another add. Don't bother with the return value
  804. * we just keep going.
  805. */
  806. ret_val = acpi_bus_start(device);
  807. acpiphp_bus_add_out:
  808. return ret_val;
  809. }
  810. /**
  811. * acpiphp_bus_trim - trim a bus from acpi subsystem
  812. * @handle: handle to acpi namespace
  813. *
  814. */
  815. int acpiphp_bus_trim(acpi_handle handle)
  816. {
  817. struct acpi_device *device;
  818. int retval;
  819. retval = acpi_bus_get_device(handle, &device);
  820. if (retval) {
  821. dbg("acpi_device not found\n");
  822. return retval;
  823. }
  824. retval = acpi_bus_trim(device, 1);
  825. if (retval)
  826. err("cannot remove from acpi list\n");
  827. return retval;
  828. }
  829. /**
  830. * enable_device - enable, configure a slot
  831. * @slot: slot to be enabled
  832. *
  833. * This function should be called per *physical slot*,
  834. * not per each slot object in ACPI namespace.
  835. *
  836. */
  837. static int enable_device(struct acpiphp_slot *slot)
  838. {
  839. struct pci_dev *dev;
  840. struct pci_bus *bus = slot->bridge->pci_bus;
  841. struct list_head *l;
  842. struct acpiphp_func *func;
  843. int retval = 0;
  844. int num, max, pass;
  845. acpi_status status;
  846. if (slot->flags & SLOT_ENABLED)
  847. goto err_exit;
  848. /* sanity check: dev should be NULL when hot-plugged in */
  849. dev = pci_get_slot(bus, PCI_DEVFN(slot->device, 0));
  850. if (dev) {
  851. /* This case shouldn't happen */
  852. err("pci_dev structure already exists.\n");
  853. pci_dev_put(dev);
  854. retval = -1;
  855. goto err_exit;
  856. }
  857. num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
  858. if (num == 0) {
  859. err("No new device found\n");
  860. retval = -1;
  861. goto err_exit;
  862. }
  863. max = acpiphp_max_busnr(bus);
  864. for (pass = 0; pass < 2; pass++) {
  865. list_for_each_entry(dev, &bus->devices, bus_list) {
  866. if (PCI_SLOT(dev->devfn) != slot->device)
  867. continue;
  868. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  869. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
  870. max = pci_scan_bridge(bus, dev, max, pass);
  871. if (pass && dev->subordinate)
  872. pci_bus_size_bridges(dev->subordinate);
  873. }
  874. }
  875. }
  876. list_for_each (l, &slot->funcs) {
  877. func = list_entry(l, struct acpiphp_func, sibling);
  878. acpiphp_bus_add(func);
  879. }
  880. pci_bus_assign_resources(bus);
  881. acpiphp_sanitize_bus(bus);
  882. pci_enable_bridges(bus);
  883. pci_bus_add_devices(bus);
  884. acpiphp_set_hpp_values(slot->bridge->handle, bus);
  885. acpiphp_configure_ioapics(slot->bridge->handle);
  886. /* associate pci_dev to our representation */
  887. list_for_each (l, &slot->funcs) {
  888. func = list_entry(l, struct acpiphp_func, sibling);
  889. func->pci_dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
  890. func->function));
  891. if (!func->pci_dev)
  892. continue;
  893. if (func->pci_dev->hdr_type != PCI_HEADER_TYPE_BRIDGE &&
  894. func->pci_dev->hdr_type != PCI_HEADER_TYPE_CARDBUS)
  895. continue;
  896. status = find_p2p_bridge(func->handle, (u32)1, bus, NULL);
  897. if (ACPI_FAILURE(status))
  898. warn("find_p2p_bridge failed (error code = 0x%x)\n",
  899. status);
  900. }
  901. slot->flags |= SLOT_ENABLED;
  902. err_exit:
  903. return retval;
  904. }
  905. /**
  906. * disable_device - disable a slot
  907. */
  908. static int disable_device(struct acpiphp_slot *slot)
  909. {
  910. int retval = 0;
  911. struct acpiphp_func *func;
  912. struct list_head *l;
  913. /* is this slot already disabled? */
  914. if (!(slot->flags & SLOT_ENABLED))
  915. goto err_exit;
  916. list_for_each (l, &slot->funcs) {
  917. func = list_entry(l, struct acpiphp_func, sibling);
  918. if (func->bridge) {
  919. /* cleanup p2p bridges under this P2P bridge */
  920. cleanup_p2p_bridge(func->bridge->handle,
  921. (u32)1, NULL, NULL);
  922. func->bridge = NULL;
  923. }
  924. acpiphp_bus_trim(func->handle);
  925. /* try to remove anyway.
  926. * acpiphp_bus_add might have been failed */
  927. if (!func->pci_dev)
  928. continue;
  929. pci_remove_bus_device(func->pci_dev);
  930. pci_dev_put(func->pci_dev);
  931. func->pci_dev = NULL;
  932. }
  933. slot->flags &= (~SLOT_ENABLED);
  934. err_exit:
  935. return retval;
  936. }
  937. /**
  938. * get_slot_status - get ACPI slot status
  939. *
  940. * if a slot has _STA for each function and if any one of them
  941. * returned non-zero status, return it
  942. *
  943. * if a slot doesn't have _STA and if any one of its functions'
  944. * configuration space is configured, return 0x0f as a _STA
  945. *
  946. * otherwise return 0
  947. */
  948. static unsigned int get_slot_status(struct acpiphp_slot *slot)
  949. {
  950. acpi_status status;
  951. unsigned long sta = 0;
  952. u32 dvid;
  953. struct list_head *l;
  954. struct acpiphp_func *func;
  955. list_for_each (l, &slot->funcs) {
  956. func = list_entry(l, struct acpiphp_func, sibling);
  957. if (func->flags & FUNC_HAS_STA) {
  958. status = acpi_evaluate_integer(func->handle, "_STA", NULL, &sta);
  959. if (ACPI_SUCCESS(status) && sta)
  960. break;
  961. } else {
  962. pci_bus_read_config_dword(slot->bridge->pci_bus,
  963. PCI_DEVFN(slot->device,
  964. func->function),
  965. PCI_VENDOR_ID, &dvid);
  966. if (dvid != 0xffffffff) {
  967. sta = ACPI_STA_ALL;
  968. break;
  969. }
  970. }
  971. }
  972. return (unsigned int)sta;
  973. }
  974. /**
  975. * acpiphp_eject_slot - physically eject the slot
  976. */
  977. static int acpiphp_eject_slot(struct acpiphp_slot *slot)
  978. {
  979. acpi_status status;
  980. struct acpiphp_func *func;
  981. struct list_head *l;
  982. struct acpi_object_list arg_list;
  983. union acpi_object arg;
  984. list_for_each (l, &slot->funcs) {
  985. func = list_entry(l, struct acpiphp_func, sibling);
  986. /* We don't want to call _EJ0 on non-existing functions. */
  987. if ((func->flags & FUNC_HAS_EJ0)) {
  988. /* _EJ0 method take one argument */
  989. arg_list.count = 1;
  990. arg_list.pointer = &arg;
  991. arg.type = ACPI_TYPE_INTEGER;
  992. arg.integer.value = 1;
  993. status = acpi_evaluate_object(func->handle, "_EJ0", &arg_list, NULL);
  994. if (ACPI_FAILURE(status)) {
  995. warn("%s: _EJ0 failed\n", __FUNCTION__);
  996. return -1;
  997. } else
  998. break;
  999. }
  1000. }
  1001. return 0;
  1002. }
  1003. /**
  1004. * acpiphp_check_bridge - re-enumerate devices
  1005. *
  1006. * Iterate over all slots under this bridge and make sure that if a
  1007. * card is present they are enabled, and if not they are disabled.
  1008. */
  1009. static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
  1010. {
  1011. struct acpiphp_slot *slot;
  1012. int retval = 0;
  1013. int enabled, disabled;
  1014. enabled = disabled = 0;
  1015. for (slot = bridge->slots; slot; slot = slot->next) {
  1016. unsigned int status = get_slot_status(slot);
  1017. if (slot->flags & SLOT_ENABLED) {
  1018. if (status == ACPI_STA_ALL)
  1019. continue;
  1020. retval = acpiphp_disable_slot(slot);
  1021. if (retval) {
  1022. err("Error occurred in disabling\n");
  1023. goto err_exit;
  1024. } else {
  1025. acpiphp_eject_slot(slot);
  1026. }
  1027. disabled++;
  1028. } else {
  1029. if (status != ACPI_STA_ALL)
  1030. continue;
  1031. retval = acpiphp_enable_slot(slot);
  1032. if (retval) {
  1033. err("Error occurred in enabling\n");
  1034. goto err_exit;
  1035. }
  1036. enabled++;
  1037. }
  1038. }
  1039. dbg("%s: %d enabled, %d disabled\n", __FUNCTION__, enabled, disabled);
  1040. err_exit:
  1041. return retval;
  1042. }
  1043. static void program_hpp(struct pci_dev *dev, struct acpiphp_bridge *bridge)
  1044. {
  1045. u16 pci_cmd, pci_bctl;
  1046. struct pci_dev *cdev;
  1047. /* Program hpp values for this device */
  1048. if (!(dev->hdr_type == PCI_HEADER_TYPE_NORMAL ||
  1049. (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE &&
  1050. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI)))
  1051. return;
  1052. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  1053. bridge->hpp.t0->cache_line_size);
  1054. pci_write_config_byte(dev, PCI_LATENCY_TIMER,
  1055. bridge->hpp.t0->latency_timer);
  1056. pci_read_config_word(dev, PCI_COMMAND, &pci_cmd);
  1057. if (bridge->hpp.t0->enable_serr)
  1058. pci_cmd |= PCI_COMMAND_SERR;
  1059. else
  1060. pci_cmd &= ~PCI_COMMAND_SERR;
  1061. if (bridge->hpp.t0->enable_perr)
  1062. pci_cmd |= PCI_COMMAND_PARITY;
  1063. else
  1064. pci_cmd &= ~PCI_COMMAND_PARITY;
  1065. pci_write_config_word(dev, PCI_COMMAND, pci_cmd);
  1066. /* Program bridge control value and child devices */
  1067. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  1068. pci_write_config_byte(dev, PCI_SEC_LATENCY_TIMER,
  1069. bridge->hpp.t0->latency_timer);
  1070. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &pci_bctl);
  1071. if (bridge->hpp.t0->enable_serr)
  1072. pci_bctl |= PCI_BRIDGE_CTL_SERR;
  1073. else
  1074. pci_bctl &= ~PCI_BRIDGE_CTL_SERR;
  1075. if (bridge->hpp.t0->enable_perr)
  1076. pci_bctl |= PCI_BRIDGE_CTL_PARITY;
  1077. else
  1078. pci_bctl &= ~PCI_BRIDGE_CTL_PARITY;
  1079. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, pci_bctl);
  1080. if (dev->subordinate) {
  1081. list_for_each_entry(cdev, &dev->subordinate->devices,
  1082. bus_list)
  1083. program_hpp(cdev, bridge);
  1084. }
  1085. }
  1086. }
  1087. static void acpiphp_set_hpp_values(acpi_handle handle, struct pci_bus *bus)
  1088. {
  1089. struct acpiphp_bridge bridge;
  1090. struct pci_dev *dev;
  1091. memset(&bridge, 0, sizeof(bridge));
  1092. bridge.handle = handle;
  1093. bridge.pci_bus = bus;
  1094. bridge.pci_dev = bus->self;
  1095. decode_hpp(&bridge);
  1096. list_for_each_entry(dev, &bus->devices, bus_list)
  1097. program_hpp(dev, &bridge);
  1098. }
  1099. /*
  1100. * Remove devices for which we could not assign resources, call
  1101. * arch specific code to fix-up the bus
  1102. */
  1103. static void acpiphp_sanitize_bus(struct pci_bus *bus)
  1104. {
  1105. struct pci_dev *dev;
  1106. int i;
  1107. unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
  1108. list_for_each_entry(dev, &bus->devices, bus_list) {
  1109. for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
  1110. struct resource *res = &dev->resource[i];
  1111. if ((res->flags & type_mask) && !res->start &&
  1112. res->end) {
  1113. /* Could not assign a required resources
  1114. * for this device, remove it */
  1115. pci_remove_bus_device(dev);
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. /* Program resources in newly inserted bridge */
  1122. static int acpiphp_configure_bridge (acpi_handle handle)
  1123. {
  1124. struct acpi_pci_id pci_id;
  1125. struct pci_bus *bus;
  1126. if (ACPI_FAILURE(acpi_get_pci_id(handle, &pci_id))) {
  1127. err("cannot get PCI domain and bus number for bridge\n");
  1128. return -EINVAL;
  1129. }
  1130. bus = pci_find_bus(pci_id.segment, pci_id.bus);
  1131. if (!bus) {
  1132. err("cannot find bus %d:%d\n",
  1133. pci_id.segment, pci_id.bus);
  1134. return -EINVAL;
  1135. }
  1136. pci_bus_size_bridges(bus);
  1137. pci_bus_assign_resources(bus);
  1138. acpiphp_sanitize_bus(bus);
  1139. acpiphp_set_hpp_values(handle, bus);
  1140. pci_enable_bridges(bus);
  1141. acpiphp_configure_ioapics(handle);
  1142. return 0;
  1143. }
  1144. static void handle_bridge_insertion(acpi_handle handle, u32 type)
  1145. {
  1146. struct acpi_device *device, *pdevice;
  1147. acpi_handle phandle;
  1148. if ((type != ACPI_NOTIFY_BUS_CHECK) &&
  1149. (type != ACPI_NOTIFY_DEVICE_CHECK)) {
  1150. err("unexpected notification type %d\n", type);
  1151. return;
  1152. }
  1153. acpi_get_parent(handle, &phandle);
  1154. if (acpi_bus_get_device(phandle, &pdevice)) {
  1155. dbg("no parent device, assuming NULL\n");
  1156. pdevice = NULL;
  1157. }
  1158. if (acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE)) {
  1159. err("cannot add bridge to acpi list\n");
  1160. return;
  1161. }
  1162. if (!acpiphp_configure_bridge(handle) &&
  1163. !acpi_bus_start(device))
  1164. add_bridge(handle);
  1165. else
  1166. err("cannot configure and start bridge\n");
  1167. }
  1168. /*
  1169. * ACPI event handlers
  1170. */
  1171. /**
  1172. * handle_hotplug_event_bridge - handle ACPI event on bridges
  1173. *
  1174. * @handle: Notify()'ed acpi_handle
  1175. * @type: Notify code
  1176. * @context: pointer to acpiphp_bridge structure
  1177. *
  1178. * handles ACPI event notification on {host,p2p} bridges
  1179. *
  1180. */
  1181. static void handle_hotplug_event_bridge(acpi_handle handle, u32 type, void *context)
  1182. {
  1183. struct acpiphp_bridge *bridge;
  1184. char objname[64];
  1185. struct acpi_buffer buffer = { .length = sizeof(objname),
  1186. .pointer = objname };
  1187. struct acpi_device *device;
  1188. if (acpi_bus_get_device(handle, &device)) {
  1189. /* This bridge must have just been physically inserted */
  1190. handle_bridge_insertion(handle, type);
  1191. return;
  1192. }
  1193. bridge = acpiphp_handle_to_bridge(handle);
  1194. if (!bridge) {
  1195. err("cannot get bridge info\n");
  1196. return;
  1197. }
  1198. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1199. switch (type) {
  1200. case ACPI_NOTIFY_BUS_CHECK:
  1201. /* bus re-enumerate */
  1202. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  1203. acpiphp_check_bridge(bridge);
  1204. break;
  1205. case ACPI_NOTIFY_DEVICE_CHECK:
  1206. /* device check */
  1207. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  1208. acpiphp_check_bridge(bridge);
  1209. break;
  1210. case ACPI_NOTIFY_DEVICE_WAKE:
  1211. /* wake event */
  1212. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  1213. break;
  1214. case ACPI_NOTIFY_EJECT_REQUEST:
  1215. /* request device eject */
  1216. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  1217. if ((bridge->type != BRIDGE_TYPE_HOST) &&
  1218. (bridge->flags & BRIDGE_HAS_EJ0)) {
  1219. struct acpiphp_slot *slot;
  1220. slot = bridge->func->slot;
  1221. if (!acpiphp_disable_slot(slot))
  1222. acpiphp_eject_slot(slot);
  1223. }
  1224. break;
  1225. case ACPI_NOTIFY_FREQUENCY_MISMATCH:
  1226. printk(KERN_ERR "Device %s cannot be configured due"
  1227. " to a frequency mismatch\n", objname);
  1228. break;
  1229. case ACPI_NOTIFY_BUS_MODE_MISMATCH:
  1230. printk(KERN_ERR "Device %s cannot be configured due"
  1231. " to a bus mode mismatch\n", objname);
  1232. break;
  1233. case ACPI_NOTIFY_POWER_FAULT:
  1234. printk(KERN_ERR "Device %s has suffered a power fault\n",
  1235. objname);
  1236. break;
  1237. default:
  1238. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1239. break;
  1240. }
  1241. }
  1242. /**
  1243. * handle_hotplug_event_func - handle ACPI event on functions (i.e. slots)
  1244. *
  1245. * @handle: Notify()'ed acpi_handle
  1246. * @type: Notify code
  1247. * @context: pointer to acpiphp_func structure
  1248. *
  1249. * handles ACPI event notification on slots
  1250. *
  1251. */
  1252. static void handle_hotplug_event_func(acpi_handle handle, u32 type, void *context)
  1253. {
  1254. struct acpiphp_func *func;
  1255. char objname[64];
  1256. struct acpi_buffer buffer = { .length = sizeof(objname),
  1257. .pointer = objname };
  1258. acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
  1259. func = (struct acpiphp_func *)context;
  1260. switch (type) {
  1261. case ACPI_NOTIFY_BUS_CHECK:
  1262. /* bus re-enumerate */
  1263. dbg("%s: Bus check notify on %s\n", __FUNCTION__, objname);
  1264. acpiphp_enable_slot(func->slot);
  1265. break;
  1266. case ACPI_NOTIFY_DEVICE_CHECK:
  1267. /* device check : re-enumerate from parent bus */
  1268. dbg("%s: Device check notify on %s\n", __FUNCTION__, objname);
  1269. acpiphp_check_bridge(func->slot->bridge);
  1270. break;
  1271. case ACPI_NOTIFY_DEVICE_WAKE:
  1272. /* wake event */
  1273. dbg("%s: Device wake notify on %s\n", __FUNCTION__, objname);
  1274. break;
  1275. case ACPI_NOTIFY_EJECT_REQUEST:
  1276. /* request device eject */
  1277. dbg("%s: Device eject notify on %s\n", __FUNCTION__, objname);
  1278. if (!(acpiphp_disable_slot(func->slot)))
  1279. acpiphp_eject_slot(func->slot);
  1280. break;
  1281. default:
  1282. warn("notify_handler: unknown event type 0x%x for %s\n", type, objname);
  1283. break;
  1284. }
  1285. }
  1286. static acpi_status
  1287. find_root_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
  1288. {
  1289. int *count = (int *)context;
  1290. if (acpi_root_bridge(handle)) {
  1291. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  1292. handle_hotplug_event_bridge, NULL);
  1293. (*count)++;
  1294. }
  1295. return AE_OK ;
  1296. }
  1297. static struct acpi_pci_driver acpi_pci_hp_driver = {
  1298. .add = add_bridge,
  1299. .remove = remove_bridge,
  1300. };
  1301. /**
  1302. * acpiphp_glue_init - initializes all PCI hotplug - ACPI glue data structures
  1303. *
  1304. */
  1305. int __init acpiphp_glue_init(void)
  1306. {
  1307. int num = 0;
  1308. acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  1309. ACPI_UINT32_MAX, find_root_bridges, &num, NULL);
  1310. if (num <= 0)
  1311. return -1;
  1312. else
  1313. acpi_pci_register_driver(&acpi_pci_hp_driver);
  1314. return 0;
  1315. }
  1316. /**
  1317. * acpiphp_glue_exit - terminates all PCI hotplug - ACPI glue data structures
  1318. *
  1319. * This function frees all data allocated in acpiphp_glue_init()
  1320. */
  1321. void __exit acpiphp_glue_exit(void)
  1322. {
  1323. acpi_pci_unregister_driver(&acpi_pci_hp_driver);
  1324. }
  1325. /**
  1326. * acpiphp_get_num_slots - count number of slots in a system
  1327. */
  1328. int __init acpiphp_get_num_slots(void)
  1329. {
  1330. struct list_head *node;
  1331. struct acpiphp_bridge *bridge;
  1332. int num_slots;
  1333. num_slots = 0;
  1334. list_for_each (node, &bridge_list) {
  1335. bridge = (struct acpiphp_bridge *)node;
  1336. dbg("Bus %04x:%02x has %d slot%s\n",
  1337. pci_domain_nr(bridge->pci_bus),
  1338. bridge->pci_bus->number, bridge->nr_slots,
  1339. bridge->nr_slots == 1 ? "" : "s");
  1340. num_slots += bridge->nr_slots;
  1341. }
  1342. dbg("Total %d slots\n", num_slots);
  1343. return num_slots;
  1344. }
  1345. #if 0
  1346. /**
  1347. * acpiphp_for_each_slot - call function for each slot
  1348. * @fn: callback function
  1349. * @data: context to be passed to callback function
  1350. *
  1351. */
  1352. static int acpiphp_for_each_slot(acpiphp_callback fn, void *data)
  1353. {
  1354. struct list_head *node;
  1355. struct acpiphp_bridge *bridge;
  1356. struct acpiphp_slot *slot;
  1357. int retval = 0;
  1358. list_for_each (node, &bridge_list) {
  1359. bridge = (struct acpiphp_bridge *)node;
  1360. for (slot = bridge->slots; slot; slot = slot->next) {
  1361. retval = fn(slot, data);
  1362. if (!retval)
  1363. goto err_exit;
  1364. }
  1365. }
  1366. err_exit:
  1367. return retval;
  1368. }
  1369. #endif
  1370. /**
  1371. * acpiphp_enable_slot - power on slot
  1372. */
  1373. int acpiphp_enable_slot(struct acpiphp_slot *slot)
  1374. {
  1375. int retval;
  1376. mutex_lock(&slot->crit_sect);
  1377. /* wake up all functions */
  1378. retval = power_on_slot(slot);
  1379. if (retval)
  1380. goto err_exit;
  1381. if (get_slot_status(slot) == ACPI_STA_ALL) {
  1382. /* configure all functions */
  1383. retval = enable_device(slot);
  1384. if (retval)
  1385. power_off_slot(slot);
  1386. } else {
  1387. dbg("%s: Slot status is not ACPI_STA_ALL\n", __FUNCTION__);
  1388. power_off_slot(slot);
  1389. }
  1390. err_exit:
  1391. mutex_unlock(&slot->crit_sect);
  1392. return retval;
  1393. }
  1394. /**
  1395. * acpiphp_disable_slot - power off slot
  1396. */
  1397. int acpiphp_disable_slot(struct acpiphp_slot *slot)
  1398. {
  1399. int retval = 0;
  1400. mutex_lock(&slot->crit_sect);
  1401. /* unconfigure all functions */
  1402. retval = disable_device(slot);
  1403. if (retval)
  1404. goto err_exit;
  1405. /* power off all functions */
  1406. retval = power_off_slot(slot);
  1407. if (retval)
  1408. goto err_exit;
  1409. err_exit:
  1410. mutex_unlock(&slot->crit_sect);
  1411. return retval;
  1412. }
  1413. /*
  1414. * slot enabled: 1
  1415. * slot disabled: 0
  1416. */
  1417. u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
  1418. {
  1419. return (slot->flags & SLOT_POWEREDON);
  1420. }
  1421. /*
  1422. * latch closed: 1
  1423. * latch open: 0
  1424. */
  1425. u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
  1426. {
  1427. unsigned int sta;
  1428. sta = get_slot_status(slot);
  1429. return (sta & ACPI_STA_SHOW_IN_UI) ? 1 : 0;
  1430. }
  1431. /*
  1432. * adapter presence : 1
  1433. * absence : 0
  1434. */
  1435. u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
  1436. {
  1437. unsigned int sta;
  1438. sta = get_slot_status(slot);
  1439. return (sta == 0) ? 0 : 1;
  1440. }
  1441. /*
  1442. * pci address (seg/bus/dev)
  1443. */
  1444. u32 acpiphp_get_address(struct acpiphp_slot *slot)
  1445. {
  1446. u32 address;
  1447. struct pci_bus *pci_bus = slot->bridge->pci_bus;
  1448. address = (pci_domain_nr(pci_bus) << 16) |
  1449. (pci_bus->number << 8) |
  1450. slot->device;
  1451. return address;
  1452. }