pciehprm_nonacpi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * PCIEHPRM NONACPI: PHP Resource Manager for Non-ACPI/Legacy platform
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include <linux/init.h>
  35. #include <asm/uaccess.h>
  36. #ifdef CONFIG_IA64
  37. #include <asm/iosapic.h>
  38. #endif
  39. #include "pciehp.h"
  40. #include "pciehprm.h"
  41. #include "pciehprm_nonacpi.h"
  42. void pciehprm_cleanup(void)
  43. {
  44. return;
  45. }
  46. int pciehprm_print_pirt(void)
  47. {
  48. return 0;
  49. }
  50. int pciehprm_get_physical_slot_number(struct controller *ctrl, u32 *sun, u8 busnum, u8 devnum)
  51. {
  52. *sun = (u8) (ctrl->first_slot);
  53. return 0;
  54. }
  55. static void print_pci_resource ( struct pci_resource *aprh)
  56. {
  57. struct pci_resource *res;
  58. for (res = aprh; res; res = res->next)
  59. dbg(" base= 0x%x length= 0x%x\n", res->base, res->length);
  60. }
  61. static void phprm_dump_func_res( struct pci_func *fun)
  62. {
  63. struct pci_func *func = fun;
  64. if (func->bus_head) {
  65. dbg(": BUS Resources:\n");
  66. print_pci_resource (func->bus_head);
  67. }
  68. if (func->io_head) {
  69. dbg(": IO Resources:\n");
  70. print_pci_resource (func->io_head);
  71. }
  72. if (func->mem_head) {
  73. dbg(": MEM Resources:\n");
  74. print_pci_resource (func->mem_head);
  75. }
  76. if (func->p_mem_head) {
  77. dbg(": PMEM Resources:\n");
  78. print_pci_resource (func->p_mem_head);
  79. }
  80. }
  81. static int phprm_get_used_resources (
  82. struct controller *ctrl,
  83. struct pci_func *func
  84. )
  85. {
  86. return pciehp_save_used_resources (ctrl, func, !DISABLE_CARD);
  87. }
  88. static int phprm_delete_resource(
  89. struct pci_resource **aprh,
  90. ulong base,
  91. ulong size)
  92. {
  93. struct pci_resource *res;
  94. struct pci_resource *prevnode;
  95. struct pci_resource *split_node;
  96. ulong tbase;
  97. pciehp_resource_sort_and_combine(aprh);
  98. for (res = *aprh; res; res = res->next) {
  99. if (res->base > base)
  100. continue;
  101. if ((res->base + res->length) < (base + size))
  102. continue;
  103. if (res->base < base) {
  104. tbase = base;
  105. if ((res->length - (tbase - res->base)) < size)
  106. continue;
  107. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  108. if (!split_node)
  109. return -ENOMEM;
  110. split_node->base = res->base;
  111. split_node->length = tbase - res->base;
  112. res->base = tbase;
  113. res->length -= split_node->length;
  114. split_node->next = res->next;
  115. res->next = split_node;
  116. }
  117. if (res->length >= size) {
  118. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  119. if (!split_node)
  120. return -ENOMEM;
  121. split_node->base = res->base + size;
  122. split_node->length = res->length - size;
  123. res->length = size;
  124. split_node->next = res->next;
  125. res->next = split_node;
  126. }
  127. if (*aprh == res) {
  128. *aprh = res->next;
  129. } else {
  130. prevnode = *aprh;
  131. while (prevnode->next != res)
  132. prevnode = prevnode->next;
  133. prevnode->next = res->next;
  134. }
  135. res->next = NULL;
  136. kfree(res);
  137. break;
  138. }
  139. return 0;
  140. }
  141. static int phprm_delete_resources(
  142. struct pci_resource **aprh,
  143. struct pci_resource *this
  144. )
  145. {
  146. struct pci_resource *res;
  147. for (res = this; res; res = res->next)
  148. phprm_delete_resource(aprh, res->base, res->length);
  149. return 0;
  150. }
  151. static int configure_existing_function(
  152. struct controller *ctrl,
  153. struct pci_func *func
  154. )
  155. {
  156. int rc;
  157. /* see how much resources the func has used. */
  158. rc = phprm_get_used_resources (ctrl, func);
  159. if (!rc) {
  160. /* subtract the resources used by the func from ctrl resources */
  161. rc = phprm_delete_resources (&ctrl->bus_head, func->bus_head);
  162. rc |= phprm_delete_resources (&ctrl->io_head, func->io_head);
  163. rc |= phprm_delete_resources (&ctrl->mem_head, func->mem_head);
  164. rc |= phprm_delete_resources (&ctrl->p_mem_head, func->p_mem_head);
  165. if (rc)
  166. warn("aCEF: cannot del used resources\n");
  167. } else
  168. err("aCEF: cannot get used resources\n");
  169. return rc;
  170. }
  171. static int pciehprm_delete_resource(
  172. struct pci_resource **aprh,
  173. ulong base,
  174. ulong size)
  175. {
  176. struct pci_resource *res;
  177. struct pci_resource *prevnode;
  178. struct pci_resource *split_node;
  179. ulong tbase;
  180. pciehp_resource_sort_and_combine(aprh);
  181. for (res = *aprh; res; res = res->next) {
  182. if (res->base > base)
  183. continue;
  184. if ((res->base + res->length) < (base + size))
  185. continue;
  186. if (res->base < base) {
  187. tbase = base;
  188. if ((res->length - (tbase - res->base)) < size)
  189. continue;
  190. split_node = (struct pci_resource *) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  191. if (!split_node)
  192. return -ENOMEM;
  193. split_node->base = res->base;
  194. split_node->length = tbase - res->base;
  195. res->base = tbase;
  196. res->length -= split_node->length;
  197. split_node->next = res->next;
  198. res->next = split_node;
  199. }
  200. if (res->length >= size) {
  201. split_node = (struct pci_resource*) kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  202. if (!split_node)
  203. return -ENOMEM;
  204. split_node->base = res->base + size;
  205. split_node->length = res->length - size;
  206. res->length = size;
  207. split_node->next = res->next;
  208. res->next = split_node;
  209. }
  210. if (*aprh == res) {
  211. *aprh = res->next;
  212. } else {
  213. prevnode = *aprh;
  214. while (prevnode->next != res)
  215. prevnode = prevnode->next;
  216. prevnode->next = res->next;
  217. }
  218. res->next = NULL;
  219. kfree(res);
  220. break;
  221. }
  222. return 0;
  223. }
  224. static int bind_pci_resources_to_slots ( struct controller *ctrl)
  225. {
  226. struct pci_func *func, new_func;
  227. int busn = ctrl->slot_bus;
  228. int devn, funn;
  229. u32 vid;
  230. for (devn = 0; devn < 32; devn++) {
  231. for (funn = 0; funn < 8; funn++) {
  232. /*
  233. if (devn == ctrl->device && funn == ctrl->function)
  234. continue;
  235. */
  236. /* find out if this entry is for an occupied slot */
  237. vid = 0xFFFFFFFF;
  238. pci_bus_read_config_dword(ctrl->pci_dev->subordinate, PCI_DEVFN(devn, funn), PCI_VENDOR_ID, &vid);
  239. if (vid != 0xFFFFFFFF) {
  240. dbg("%s: vid = %x bus %x dev %x fun %x\n", __FUNCTION__,
  241. vid, busn, devn, funn);
  242. func = pciehp_slot_find(busn, devn, funn);
  243. dbg("%s: func = %p\n", __FUNCTION__,func);
  244. if (!func) {
  245. memset(&new_func, 0, sizeof(struct pci_func));
  246. new_func.bus = busn;
  247. new_func.device = devn;
  248. new_func.function = funn;
  249. new_func.is_a_board = 1;
  250. configure_existing_function(ctrl, &new_func);
  251. phprm_dump_func_res(&new_func);
  252. } else {
  253. configure_existing_function(ctrl, func);
  254. phprm_dump_func_res(func);
  255. }
  256. dbg("aCCF:existing PCI 0x%x Func ResourceDump\n", ctrl->bus);
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. static void phprm_dump_ctrl_res( struct controller *ctlr)
  263. {
  264. struct controller *ctrl = ctlr;
  265. if (ctrl->bus_head) {
  266. dbg(": BUS Resources:\n");
  267. print_pci_resource (ctrl->bus_head);
  268. }
  269. if (ctrl->io_head) {
  270. dbg(": IO Resources:\n");
  271. print_pci_resource (ctrl->io_head);
  272. }
  273. if (ctrl->mem_head) {
  274. dbg(": MEM Resources:\n");
  275. print_pci_resource (ctrl->mem_head);
  276. }
  277. if (ctrl->p_mem_head) {
  278. dbg(": PMEM Resources:\n");
  279. print_pci_resource (ctrl->p_mem_head);
  280. }
  281. }
  282. /*
  283. * phprm_find_available_resources
  284. *
  285. * Finds available memory, IO, and IRQ resources for programming
  286. * devices which may be added to the system
  287. * this function is for hot plug ADD!
  288. *
  289. * returns 0 if success
  290. */
  291. int pciehprm_find_available_resources(struct controller *ctrl)
  292. {
  293. struct pci_func func;
  294. u32 rc;
  295. memset(&func, 0, sizeof(struct pci_func));
  296. func.bus = ctrl->bus;
  297. func.device = ctrl->device;
  298. func.function = ctrl->function;
  299. func.is_a_board = 1;
  300. /* Get resources for this PCI bridge */
  301. rc = pciehp_save_used_resources (ctrl, &func, !DISABLE_CARD);
  302. dbg("%s: pciehp_save_used_resources rc = %d\n", __FUNCTION__, rc);
  303. if (func.mem_head)
  304. func.mem_head->next = ctrl->mem_head;
  305. ctrl->mem_head = func.mem_head;
  306. if (func.p_mem_head)
  307. func.p_mem_head->next = ctrl->p_mem_head;
  308. ctrl->p_mem_head = func.p_mem_head;
  309. if (func.io_head)
  310. func.io_head->next = ctrl->io_head;
  311. ctrl->io_head = func.io_head;
  312. if(func.bus_head)
  313. func.bus_head->next = ctrl->bus_head;
  314. ctrl->bus_head = func.bus_head;
  315. if (ctrl->bus_head)
  316. pciehprm_delete_resource(&ctrl->bus_head, ctrl->pci_dev->subordinate->number, 1);
  317. dbg("%s:pre-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  318. phprm_dump_ctrl_res(ctrl);
  319. dbg("%s: before bind_pci_resources_to slots\n", __FUNCTION__);
  320. bind_pci_resources_to_slots (ctrl);
  321. dbg("%s:post-Bind PCI 0x%x Ctrl Resource Dump\n", __FUNCTION__, ctrl->bus);
  322. phprm_dump_ctrl_res(ctrl);
  323. return (rc);
  324. }
  325. int pciehprm_set_hpp(
  326. struct controller *ctrl,
  327. struct pci_func *func,
  328. u8 card_type)
  329. {
  330. u32 rc;
  331. u8 temp_byte;
  332. struct pci_bus lpci_bus, *pci_bus;
  333. unsigned int devfn;
  334. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  335. pci_bus = &lpci_bus;
  336. pci_bus->number = func->bus;
  337. devfn = PCI_DEVFN(func->device, func->function);
  338. temp_byte = 0x40; /* hard coded value for LT */
  339. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  340. /* set subordinate Latency Timer */
  341. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_SEC_LATENCY_TIMER, temp_byte);
  342. if (rc) {
  343. dbg("%s: set secondary LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__,
  344. func->bus, func->device, func->function);
  345. return rc;
  346. }
  347. }
  348. /* set base Latency Timer */
  349. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_LATENCY_TIMER, temp_byte);
  350. if (rc) {
  351. dbg("%s: set LT error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  352. return rc;
  353. }
  354. /* set Cache Line size */
  355. temp_byte = 0x08; /* hard coded value for CLS */
  356. rc = pci_bus_write_config_byte(pci_bus, devfn, PCI_CACHE_LINE_SIZE, temp_byte);
  357. if (rc) {
  358. dbg("%s: set CLS error. b:d:f(%02x:%02x:%02x)\n", __FUNCTION__, func->bus, func->device, func->function);
  359. }
  360. /* set enable_perr */
  361. /* set enable_serr */
  362. return rc;
  363. }
  364. void pciehprm_enable_card(
  365. struct controller *ctrl,
  366. struct pci_func *func,
  367. u8 card_type)
  368. {
  369. u16 command, bcommand;
  370. struct pci_bus lpci_bus, *pci_bus;
  371. unsigned int devfn;
  372. int rc;
  373. memcpy(&lpci_bus, ctrl->pci_bus, sizeof(lpci_bus));
  374. pci_bus = &lpci_bus;
  375. pci_bus->number = func->bus;
  376. devfn = PCI_DEVFN(func->device, func->function);
  377. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &command);
  378. command |= PCI_COMMAND_PARITY | PCI_COMMAND_SERR
  379. | PCI_COMMAND_MASTER | PCI_COMMAND_INVALIDATE
  380. | PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  381. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
  382. if (card_type == PCI_HEADER_TYPE_BRIDGE) {
  383. rc = pci_bus_read_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, &bcommand);
  384. bcommand |= PCI_BRIDGE_CTL_PARITY | PCI_BRIDGE_CTL_SERR
  385. | PCI_BRIDGE_CTL_NO_ISA;
  386. rc = pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, bcommand);
  387. }
  388. }
  389. static int legacy_pciehprm_init_pci(void)
  390. {
  391. return 0;
  392. }
  393. int pciehprm_init(enum php_ctlr_type ctrl_type)
  394. {
  395. int retval;
  396. switch (ctrl_type) {
  397. case PCI:
  398. retval = legacy_pciehprm_init_pci();
  399. break;
  400. default:
  401. retval = -ENODEV;
  402. break;
  403. }
  404. return retval;
  405. }