shpchprm_acpi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * SHPCHPRM ACPI: PHP Resource Manager for ACPI platform
  3. *
  4. * Copyright (C) 2003-2004 Intel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Send feedback to <kristen.c.accardi@intel.com>
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/types.h>
  30. #include <linux/pci.h>
  31. #include <linux/init.h>
  32. #include <linux/acpi.h>
  33. #include <linux/efi.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/system.h>
  36. #include <acpi/acpi.h>
  37. #include <acpi/acpi_bus.h>
  38. #include <acpi/actypes.h>
  39. #include "shpchp.h"
  40. #include "shpchprm.h"
  41. #define PCI_MAX_BUS 0x100
  42. #define ACPI_STA_DEVICE_PRESENT 0x01
  43. #define METHOD_NAME__SUN "_SUN"
  44. #define METHOD_NAME__HPP "_HPP"
  45. #define METHOD_NAME_OSHP "OSHP"
  46. #define PHP_RES_BUS 0xA0
  47. #define PHP_RES_IO 0xA1
  48. #define PHP_RES_MEM 0xA2
  49. #define PHP_RES_PMEM 0xA3
  50. #define BRIDGE_TYPE_P2P 0x00
  51. #define BRIDGE_TYPE_HOST 0x01
  52. /* this should go to drivers/acpi/include/ */
  53. struct acpi__hpp {
  54. u8 cache_line_size;
  55. u8 latency_timer;
  56. u8 enable_serr;
  57. u8 enable_perr;
  58. };
  59. struct acpi_php_slot {
  60. struct acpi_php_slot *next;
  61. struct acpi_bridge *bridge;
  62. acpi_handle handle;
  63. int seg;
  64. int bus;
  65. int dev;
  66. int fun;
  67. u32 sun;
  68. void *slot_ops; /* _STA, _EJx, etc */
  69. struct slot *slot;
  70. }; /* per func */
  71. struct acpi_bridge {
  72. struct acpi_bridge *parent;
  73. struct acpi_bridge *next;
  74. struct acpi_bridge *child;
  75. acpi_handle handle;
  76. int seg;
  77. int pbus; /* pdev->bus->number */
  78. int pdevice; /* PCI_SLOT(pdev->devfn) */
  79. int pfunction; /* PCI_DEVFN(pdev->devfn) */
  80. int bus; /* pdev->subordinate->number */
  81. struct acpi__hpp *_hpp;
  82. struct acpi_php_slot *slots;
  83. int scanned;
  84. int type;
  85. };
  86. static struct acpi_bridge *acpi_bridges_head;
  87. static u8 * acpi_path_name( acpi_handle handle)
  88. {
  89. acpi_status status;
  90. static u8 path_name[ACPI_PATHNAME_MAX];
  91. struct acpi_buffer ret_buf = { ACPI_PATHNAME_MAX, path_name };
  92. memset(path_name, 0, sizeof (path_name));
  93. status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &ret_buf);
  94. if (ACPI_FAILURE(status))
  95. return NULL;
  96. else
  97. return path_name;
  98. }
  99. static void acpi_get__hpp ( struct acpi_bridge *ab);
  100. static void acpi_run_oshp ( struct acpi_bridge *ab);
  101. static int acpi_add_slot_to_php_slots(
  102. struct acpi_bridge *ab,
  103. int bus_num,
  104. acpi_handle handle,
  105. u32 adr,
  106. u32 sun
  107. )
  108. {
  109. struct acpi_php_slot *aps;
  110. static long samesun = -1;
  111. aps = (struct acpi_php_slot *) kmalloc (sizeof(struct acpi_php_slot), GFP_KERNEL);
  112. if (!aps) {
  113. err ("acpi_shpchprm: alloc for aps fail\n");
  114. return -1;
  115. }
  116. memset(aps, 0, sizeof(struct acpi_php_slot));
  117. aps->handle = handle;
  118. aps->bus = bus_num;
  119. aps->dev = (adr >> 16) & 0xffff;
  120. aps->fun = adr & 0xffff;
  121. aps->sun = sun;
  122. aps->next = ab->slots; /* cling to the bridge */
  123. aps->bridge = ab;
  124. ab->slots = aps;
  125. ab->scanned += 1;
  126. if (!ab->_hpp)
  127. acpi_get__hpp(ab);
  128. acpi_run_oshp(ab);
  129. if (sun != samesun) {
  130. info("acpi_shpchprm: Slot sun(%x) at s:b:d:f=0x%02x:%02x:%02x:%02x\n", aps->sun, ab->seg,
  131. aps->bus, aps->dev, aps->fun);
  132. samesun = sun;
  133. }
  134. return 0;
  135. }
  136. static void acpi_get__hpp ( struct acpi_bridge *ab)
  137. {
  138. acpi_status status;
  139. u8 nui[4];
  140. struct acpi_buffer ret_buf = { 0, NULL};
  141. union acpi_object *ext_obj, *package;
  142. u8 *path_name = acpi_path_name(ab->handle);
  143. int i, len = 0;
  144. /* get _hpp */
  145. status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
  146. switch (status) {
  147. case AE_BUFFER_OVERFLOW:
  148. ret_buf.pointer = kmalloc (ret_buf.length, GFP_KERNEL);
  149. if (!ret_buf.pointer) {
  150. err ("acpi_shpchprm:%s alloc for _HPP fail\n", path_name);
  151. return;
  152. }
  153. status = acpi_evaluate_object(ab->handle, METHOD_NAME__HPP, NULL, &ret_buf);
  154. if (ACPI_SUCCESS(status))
  155. break;
  156. default:
  157. if (ACPI_FAILURE(status)) {
  158. err("acpi_shpchprm:%s _HPP fail=0x%x\n", path_name, status);
  159. return;
  160. }
  161. }
  162. ext_obj = (union acpi_object *) ret_buf.pointer;
  163. if (ext_obj->type != ACPI_TYPE_PACKAGE) {
  164. err ("acpi_shpchprm:%s _HPP obj not a package\n", path_name);
  165. goto free_and_return;
  166. }
  167. len = ext_obj->package.count;
  168. package = (union acpi_object *) ret_buf.pointer;
  169. for ( i = 0; (i < len) || (i < 4); i++) {
  170. ext_obj = (union acpi_object *) &package->package.elements[i];
  171. switch (ext_obj->type) {
  172. case ACPI_TYPE_INTEGER:
  173. nui[i] = (u8)ext_obj->integer.value;
  174. break;
  175. default:
  176. err ("acpi_shpchprm:%s _HPP obj type incorrect\n", path_name);
  177. goto free_and_return;
  178. }
  179. }
  180. ab->_hpp = kmalloc (sizeof (struct acpi__hpp), GFP_KERNEL);
  181. if (!ab->_hpp) {
  182. err ("acpi_shpchprm:%s alloc for _HPP failed\n", path_name);
  183. goto free_and_return;
  184. }
  185. memset(ab->_hpp, 0, sizeof(struct acpi__hpp));
  186. ab->_hpp->cache_line_size = nui[0];
  187. ab->_hpp->latency_timer = nui[1];
  188. ab->_hpp->enable_serr = nui[2];
  189. ab->_hpp->enable_perr = nui[3];
  190. dbg(" _HPP: cache_line_size=0x%x\n", ab->_hpp->cache_line_size);
  191. dbg(" _HPP: latency timer =0x%x\n", ab->_hpp->latency_timer);
  192. dbg(" _HPP: enable SERR =0x%x\n", ab->_hpp->enable_serr);
  193. dbg(" _HPP: enable PERR =0x%x\n", ab->_hpp->enable_perr);
  194. free_and_return:
  195. kfree(ret_buf.pointer);
  196. }
  197. static void acpi_run_oshp ( struct acpi_bridge *ab)
  198. {
  199. acpi_status status;
  200. u8 *path_name = acpi_path_name(ab->handle);
  201. /* run OSHP */
  202. status = acpi_evaluate_object(ab->handle, METHOD_NAME_OSHP, NULL, NULL);
  203. if (ACPI_FAILURE(status)) {
  204. err("acpi_pciehprm:%s OSHP fails=0x%x\n", path_name, status);
  205. } else
  206. dbg("acpi_pciehprm:%s OSHP passes =0x%x\n", path_name, status);
  207. return;
  208. }
  209. /* find acpi_bridge downword from ab. */
  210. static struct acpi_bridge *
  211. find_acpi_bridge_by_bus(
  212. struct acpi_bridge *ab,
  213. int seg,
  214. int bus /* pdev->subordinate->number */
  215. )
  216. {
  217. struct acpi_bridge *lab = NULL;
  218. if (!ab)
  219. return NULL;
  220. if ((ab->bus == bus) && (ab->seg == seg))
  221. return ab;
  222. if (ab->child)
  223. lab = find_acpi_bridge_by_bus(ab->child, seg, bus);
  224. if (!lab)
  225. if (ab->next)
  226. lab = find_acpi_bridge_by_bus(ab->next, seg, bus);
  227. return lab;
  228. }
  229. /*
  230. * Build a device tree of ACPI PCI Bridges
  231. */
  232. static void shpchprm_acpi_register_a_bridge (
  233. struct acpi_bridge **head,
  234. struct acpi_bridge *pab, /* parent bridge to which child bridge is added */
  235. struct acpi_bridge *cab /* child bridge to add */
  236. )
  237. {
  238. struct acpi_bridge *lpab;
  239. struct acpi_bridge *lcab;
  240. lpab = find_acpi_bridge_by_bus(*head, pab->seg, pab->bus);
  241. if (!lpab) {
  242. if (!(pab->type & BRIDGE_TYPE_HOST))
  243. warn("PCI parent bridge s:b(%x:%x) not in list.\n", pab->seg, pab->bus);
  244. pab->next = *head;
  245. *head = pab;
  246. lpab = pab;
  247. }
  248. if ((cab->type & BRIDGE_TYPE_HOST) && (pab == cab))
  249. return;
  250. lcab = find_acpi_bridge_by_bus(*head, cab->seg, cab->bus);
  251. if (lcab) {
  252. if ((pab->bus != lcab->parent->bus) || (lcab->bus != cab->bus))
  253. err("PCI child bridge s:b(%x:%x) in list with diff parent.\n", cab->seg, cab->bus);
  254. return;
  255. } else
  256. lcab = cab;
  257. lcab->parent = lpab;
  258. lcab->next = lpab->child;
  259. lpab->child = lcab;
  260. }
  261. static acpi_status shpchprm_acpi_build_php_slots_callback(
  262. acpi_handle handle,
  263. u32 Level,
  264. void *context,
  265. void **retval
  266. )
  267. {
  268. ulong bus_num;
  269. ulong seg_num;
  270. ulong sun, adr;
  271. ulong padr = 0;
  272. acpi_handle phandle = NULL;
  273. struct acpi_bridge *pab = (struct acpi_bridge *)context;
  274. struct acpi_bridge *lab;
  275. acpi_status status;
  276. u8 *path_name = acpi_path_name(handle);
  277. /* get _SUN */
  278. status = acpi_evaluate_integer(handle, METHOD_NAME__SUN, NULL, &sun);
  279. switch(status) {
  280. case AE_NOT_FOUND:
  281. return AE_OK;
  282. default:
  283. if (ACPI_FAILURE(status)) {
  284. err("acpi_shpchprm:%s _SUN fail=0x%x\n", path_name, status);
  285. return status;
  286. }
  287. }
  288. /* get _ADR. _ADR must exist if _SUN exists */
  289. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  290. if (ACPI_FAILURE(status)) {
  291. err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
  292. return status;
  293. }
  294. dbg("acpi_shpchprm:%s sun=0x%08x adr=0x%08x\n", path_name, (u32)sun, (u32)adr);
  295. status = acpi_get_parent(handle, &phandle);
  296. if (ACPI_FAILURE(status)) {
  297. err("acpi_shpchprm:%s get_parent fail=0x%x\n", path_name, status);
  298. return (status);
  299. }
  300. bus_num = pab->bus;
  301. seg_num = pab->seg;
  302. if (pab->bus == bus_num) {
  303. lab = pab;
  304. } else {
  305. dbg("WARN: pab is not parent\n");
  306. lab = find_acpi_bridge_by_bus(pab, seg_num, bus_num);
  307. if (!lab) {
  308. dbg("acpi_shpchprm: alloc new P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
  309. lab = (struct acpi_bridge *)kmalloc(sizeof(struct acpi_bridge), GFP_KERNEL);
  310. if (!lab) {
  311. err("acpi_shpchprm: alloc for ab fail\n");
  312. return AE_NO_MEMORY;
  313. }
  314. memset(lab, 0, sizeof(struct acpi_bridge));
  315. lab->handle = phandle;
  316. lab->pbus = pab->bus;
  317. lab->pdevice = (int)(padr >> 16) & 0xffff;
  318. lab->pfunction = (int)(padr & 0xffff);
  319. lab->bus = (int)bus_num;
  320. lab->scanned = 0;
  321. lab->type = BRIDGE_TYPE_P2P;
  322. shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, lab);
  323. } else
  324. dbg("acpi_shpchprm: found P2P bridge(%x) for sun(%08x)\n", (u32)bus_num, (u32)sun);
  325. }
  326. acpi_add_slot_to_php_slots(lab, (int)bus_num, handle, (u32)adr, (u32)sun);
  327. return (status);
  328. }
  329. static int shpchprm_acpi_build_php_slots(
  330. struct acpi_bridge *ab,
  331. u32 depth
  332. )
  333. {
  334. acpi_status status;
  335. u8 *path_name = acpi_path_name(ab->handle);
  336. /* Walk down this pci bridge to get _SUNs if any behind P2P */
  337. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  338. ab->handle,
  339. depth,
  340. shpchprm_acpi_build_php_slots_callback,
  341. ab,
  342. NULL );
  343. if (ACPI_FAILURE(status)) {
  344. dbg("acpi_shpchprm:%s walk for _SUN on pci bridge seg:bus(%x:%x) fail=0x%x\n", path_name, ab->seg, ab->bus, status);
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. static void build_a_bridge(
  350. struct acpi_bridge *pab,
  351. struct acpi_bridge *ab
  352. )
  353. {
  354. u8 *path_name = acpi_path_name(ab->handle);
  355. shpchprm_acpi_register_a_bridge (&acpi_bridges_head, pab, ab);
  356. switch (ab->type) {
  357. case BRIDGE_TYPE_HOST:
  358. dbg("acpi_shpchprm: Registered PCI HOST Bridge(%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
  359. ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
  360. break;
  361. case BRIDGE_TYPE_P2P:
  362. dbg("acpi_shpchprm: Registered PCI P2P Bridge(%02x-%02x) on s:b:d:f(%02x:%02x:%02x:%02x) [%s]\n",
  363. ab->pbus, ab->bus, ab->seg, ab->pbus, ab->pdevice, ab->pfunction, path_name);
  364. break;
  365. };
  366. /* build any immediate PHP slots under this pci bridge */
  367. shpchprm_acpi_build_php_slots(ab, 1);
  368. }
  369. static struct acpi_bridge * add_p2p_bridge(
  370. acpi_handle handle,
  371. struct acpi_bridge *pab, /* parent */
  372. ulong adr
  373. )
  374. {
  375. struct acpi_bridge *ab;
  376. struct pci_dev *pdev;
  377. ulong devnum, funcnum;
  378. u8 *path_name = acpi_path_name(handle);
  379. ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
  380. if (!ab) {
  381. err("acpi_shpchprm: alloc for ab fail\n");
  382. return NULL;
  383. }
  384. memset(ab, 0, sizeof(struct acpi_bridge));
  385. devnum = (adr >> 16) & 0xffff;
  386. funcnum = adr & 0xffff;
  387. pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
  388. if (!pdev || !pdev->subordinate) {
  389. err("acpi_shpchprm:%s is not a P2P Bridge\n", path_name);
  390. kfree(ab);
  391. return NULL;
  392. }
  393. ab->handle = handle;
  394. ab->seg = pab->seg;
  395. ab->pbus = pab->bus; /* or pdev->bus->number */
  396. ab->pdevice = devnum; /* or PCI_SLOT(pdev->devfn) */
  397. ab->pfunction = funcnum; /* or PCI_FUNC(pdev->devfn) */
  398. ab->bus = pdev->subordinate->number;
  399. ab->scanned = 0;
  400. ab->type = BRIDGE_TYPE_P2P;
  401. dbg("acpi_shpchprm: P2P(%x-%x) on pci=b:d:f(%x:%x:%x) acpi=b:d:f(%x:%x:%x) [%s]\n",
  402. pab->bus, ab->bus, pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  403. pab->bus, (u32)devnum, (u32)funcnum, path_name);
  404. build_a_bridge(pab, ab);
  405. return ab;
  406. }
  407. static acpi_status scan_p2p_bridge(
  408. acpi_handle handle,
  409. u32 Level,
  410. void *context,
  411. void **retval
  412. )
  413. {
  414. struct acpi_bridge *pab = (struct acpi_bridge *)context;
  415. struct acpi_bridge *ab;
  416. acpi_status status;
  417. ulong adr = 0;
  418. u8 *path_name = acpi_path_name(handle);
  419. ulong devnum, funcnum;
  420. struct pci_dev *pdev;
  421. /* get device, function */
  422. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  423. if (ACPI_FAILURE(status)) {
  424. if (status != AE_NOT_FOUND)
  425. err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
  426. return AE_OK;
  427. }
  428. devnum = (adr >> 16) & 0xffff;
  429. funcnum = adr & 0xffff;
  430. pdev = pci_find_slot(pab->bus, PCI_DEVFN(devnum, funcnum));
  431. if (!pdev)
  432. return AE_OK;
  433. if (!pdev->subordinate)
  434. return AE_OK;
  435. ab = add_p2p_bridge(handle, pab, adr);
  436. if (ab) {
  437. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  438. handle,
  439. (u32)1,
  440. scan_p2p_bridge,
  441. ab,
  442. NULL);
  443. if (ACPI_FAILURE(status))
  444. dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
  445. }
  446. return AE_OK;
  447. }
  448. static struct acpi_bridge * add_host_bridge(
  449. acpi_handle handle,
  450. ulong segnum,
  451. ulong busnum
  452. )
  453. {
  454. ulong adr = 0;
  455. acpi_status status;
  456. struct acpi_bridge *ab;
  457. u8 *path_name = acpi_path_name(handle);
  458. /* get device, function: host br adr is always 0000 though. */
  459. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr);
  460. if (ACPI_FAILURE(status)) {
  461. err("acpi_shpchprm:%s _ADR fail=0x%x\n", path_name, status);
  462. return NULL;
  463. }
  464. dbg("acpi_shpchprm: ROOT PCI seg(0x%x)bus(0x%x)dev(0x%x)func(0x%x) [%s]\n", (u32)segnum, (u32)busnum,
  465. (u32)(adr >> 16) & 0xffff, (u32)adr & 0xffff, path_name);
  466. ab = (struct acpi_bridge *) kmalloc (sizeof(struct acpi_bridge), GFP_KERNEL);
  467. if (!ab) {
  468. err("acpi_shpchprm: alloc for ab fail\n");
  469. return NULL;
  470. }
  471. memset(ab, 0, sizeof(struct acpi_bridge));
  472. ab->handle = handle;
  473. ab->seg = (int)segnum;
  474. ab->bus = ab->pbus = (int)busnum;
  475. ab->pdevice = (int)(adr >> 16) & 0xffff;
  476. ab->pfunction = (int)(adr & 0xffff);
  477. ab->scanned = 0;
  478. ab->type = BRIDGE_TYPE_HOST;
  479. build_a_bridge(ab, ab);
  480. return ab;
  481. }
  482. static acpi_status acpi_scan_from_root_pci_callback (
  483. acpi_handle handle,
  484. u32 Level,
  485. void *context,
  486. void **retval
  487. )
  488. {
  489. ulong segnum = 0;
  490. ulong busnum = 0;
  491. acpi_status status;
  492. struct acpi_bridge *ab;
  493. u8 *path_name = acpi_path_name(handle);
  494. /* get bus number of this pci root bridge */
  495. status = acpi_evaluate_integer(handle, METHOD_NAME__SEG, NULL, &segnum);
  496. if (ACPI_FAILURE(status)) {
  497. if (status != AE_NOT_FOUND) {
  498. err("acpi_shpchprm:%s evaluate _SEG fail=0x%x\n", path_name, status);
  499. return status;
  500. }
  501. segnum = 0;
  502. }
  503. /* get bus number of this pci root bridge */
  504. status = acpi_evaluate_integer(handle, METHOD_NAME__BBN, NULL, &busnum);
  505. if (ACPI_FAILURE(status)) {
  506. err("acpi_shpchprm:%s evaluate _BBN fail=0x%x\n", path_name, status);
  507. return (status);
  508. }
  509. ab = add_host_bridge(handle, segnum, busnum);
  510. if (ab) {
  511. status = acpi_walk_namespace ( ACPI_TYPE_DEVICE,
  512. handle,
  513. 1,
  514. scan_p2p_bridge,
  515. ab,
  516. NULL);
  517. if (ACPI_FAILURE(status))
  518. dbg("acpi_shpchprm:%s find_p2p fail=0x%x\n", path_name, status);
  519. }
  520. return AE_OK;
  521. }
  522. static int shpchprm_acpi_scan_pci (void)
  523. {
  524. acpi_status status;
  525. /*
  526. * TBD: traverse LDM device tree with the help of
  527. * unified ACPI augmented for php device population.
  528. */
  529. status = acpi_get_devices ( PCI_ROOT_HID_STRING,
  530. acpi_scan_from_root_pci_callback,
  531. NULL,
  532. NULL );
  533. if (ACPI_FAILURE(status)) {
  534. err("acpi_shpchprm:get_device PCI ROOT HID fail=0x%x\n", status);
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. int shpchprm_init(enum php_ctlr_type ctlr_type)
  540. {
  541. int rc;
  542. if (ctlr_type != PCI)
  543. return -ENODEV;
  544. dbg("shpchprm ACPI init <enter>\n");
  545. acpi_bridges_head = NULL;
  546. /* construct PCI bus:device tree of acpi_handles */
  547. rc = shpchprm_acpi_scan_pci();
  548. if (rc)
  549. return rc;
  550. dbg("shpchprm ACPI init %s\n", (rc)?"fail":"success");
  551. return rc;
  552. }
  553. static void free_a_slot(struct acpi_php_slot *aps)
  554. {
  555. dbg(" free a php func of slot(0x%02x) on PCI b:d:f=0x%02x:%02x:%02x\n", aps->sun, aps->bus, aps->dev, aps->fun);
  556. kfree(aps);
  557. }
  558. static void free_a_bridge( struct acpi_bridge *ab)
  559. {
  560. struct acpi_php_slot *aps, *next;
  561. switch (ab->type) {
  562. case BRIDGE_TYPE_HOST:
  563. dbg("Free ACPI PCI HOST Bridge(%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
  564. ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
  565. break;
  566. case BRIDGE_TYPE_P2P:
  567. dbg("Free ACPI PCI P2P Bridge(%x-%x) [%s] on s:b:d:f(%x:%x:%x:%x)\n",
  568. ab->pbus, ab->bus, acpi_path_name(ab->handle), ab->seg, ab->pbus, ab->pdevice, ab->pfunction);
  569. break;
  570. };
  571. /* free slots first */
  572. for (aps = ab->slots; aps; aps = next) {
  573. next = aps->next;
  574. free_a_slot(aps);
  575. }
  576. kfree(ab);
  577. }
  578. static void shpchprm_free_bridges ( struct acpi_bridge *ab)
  579. {
  580. if (!ab)
  581. return;
  582. if (ab->child)
  583. shpchprm_free_bridges (ab->child);
  584. if (ab->next)
  585. shpchprm_free_bridges (ab->next);
  586. free_a_bridge(ab);
  587. }
  588. void shpchprm_cleanup(void)
  589. {
  590. shpchprm_free_bridges (acpi_bridges_head);
  591. }
  592. static int get_number_of_slots (
  593. struct acpi_bridge *ab,
  594. int selfonly
  595. )
  596. {
  597. struct acpi_php_slot *aps;
  598. int prev_slot = -1;
  599. int slot_num = 0;
  600. for ( aps = ab->slots; aps; aps = aps->next)
  601. if (aps->dev != prev_slot) {
  602. prev_slot = aps->dev;
  603. slot_num++;
  604. }
  605. if (ab->child)
  606. slot_num += get_number_of_slots (ab->child, 0);
  607. if (selfonly)
  608. return slot_num;
  609. if (ab->next)
  610. slot_num += get_number_of_slots (ab->next, 0);
  611. return slot_num;
  612. }
  613. static struct acpi_php_slot * get_acpi_slot (
  614. struct acpi_bridge *ab,
  615. u32 sun
  616. )
  617. {
  618. struct acpi_php_slot *aps = NULL;
  619. for ( aps = ab->slots; aps; aps = aps->next)
  620. if (aps->sun == sun)
  621. return aps;
  622. if (!aps && ab->child) {
  623. aps = (struct acpi_php_slot *)get_acpi_slot (ab->child, sun);
  624. if (aps)
  625. return aps;
  626. }
  627. if (!aps && ab->next) {
  628. aps = (struct acpi_php_slot *)get_acpi_slot (ab->next, sun);
  629. if (aps)
  630. return aps;
  631. }
  632. return aps;
  633. }
  634. int shpchprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
  635. {
  636. int offset = devnum - ctrl->slot_device_offset;
  637. dbg("%s: ctrl->slot_num_inc %d, offset %d\n", __FUNCTION__, ctrl->slot_num_inc, offset);
  638. *sun = (u8) (ctrl->first_slot + ctrl->slot_num_inc *offset);
  639. return 0;
  640. }
  641. int shpchprm_set_hpp(
  642. struct controller *ctrl,
  643. struct pci_func *func,
  644. u8 card_type
  645. )
  646. {
  647. struct acpi_bridge *ab;
  648. struct pci_bus lpci_bus, *pci_bus;
  649. int rc = 0;
  650. unsigned int devfn;
  651. u8 cls= 0x08; /* default cache line size */
  652. u8 lt = 0x40; /* default latency timer */
  653. u8 ep = 0;
  654. u8 es = 0;
  655. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  656. pci_bus = &lpci_bus;
  657. pci_bus->number = func->bus;
  658. devfn = PCI_DEVFN(func->device, func->function);
  659. ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->slot_bus);
  660. if (ab) {
  661. if (ab->_hpp) {
  662. lt = (u8)ab->_hpp->latency_timer;
  663. cls = (u8)ab->_hpp->cache_line_size;
  664. ep = (u8)ab->_hpp->enable_perr;
  665. es = (u8)ab->_hpp->enable_serr;
  666. } else
  667. dbg("_hpp: no _hpp for B/D/F=%#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
  668. } else
  669. dbg("_hpp: no acpi bridge for B/D/F = %#x/%#x/%#x. use default value\n", func->bus, func->device, func->function);
  670. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  671. /* set subordinate Latency Timer */
  672. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, lt);
  673. }
  674. /* set base Latency Timer */
  675. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, lt);
  676. dbg(" set latency timer =0x%02x: %x\n", lt, rc);
  677. rc |= pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, cls);
  678. dbg(" set cache_line_size=0x%02x: %x\n", cls, rc);
  679. return rc;
  680. }
  681. void shpchprm_enable_card(
  682. struct controller *ctrl,
  683. struct pci_func *func,
  684. u8 card_type)
  685. {
  686. u16 command, cmd, bcommand, bcmd;
  687. struct pci_bus lpci_bus, *pci_bus;
  688. struct acpi_bridge *ab;
  689. unsigned int devfn;
  690. int rc;
  691. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  692. pci_bus = &lpci_bus;
  693. pci_bus->number = func->bus;
  694. devfn = PCI_DEVFN(func->device, func->function);
  695. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
  696. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  697. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
  698. }
  699. cmd = command = command | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  700. | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  701. bcmd = bcommand = bcommand | PCI_BRIDGE_CTL_NO_ISA;
  702. ab = find_acpi_bridge_by_bus(acpi_bridges_head, ctrl->seg, ctrl->slot_bus);
  703. if (ab) {
  704. if (ab->_hpp) {
  705. if (ab->_hpp->enable_perr) {
  706. command |= PCI_COMMAND_PARITY;
  707. bcommand |= PCI_BRIDGE_CTL_PARITY;
  708. } else {
  709. command &= ~PCI_COMMAND_PARITY;
  710. bcommand &= ~PCI_BRIDGE_CTL_PARITY;
  711. }
  712. if (ab->_hpp->enable_serr) {
  713. command |= PCI_COMMAND_SERR;
  714. bcommand |= PCI_BRIDGE_CTL_SERR;
  715. } else {
  716. command &= ~PCI_COMMAND_SERR;
  717. bcommand &= ~PCI_BRIDGE_CTL_SERR;
  718. }
  719. } else
  720. dbg("no _hpp for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
  721. } else
  722. dbg("no acpi bridge for B/D/F = %#x/%#x/%#x.\n", func->bus, func->device, func->function);
  723. if (command != cmd) {
  724. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  725. }
  726. if ((card_type == PCI_HEADER_TYPE_BRIDGE) && (bcommand != bcmd)) {
  727. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
  728. }
  729. }