hw_phb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /* This file is part of the program psim.
  2. Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _HW_PHB_C_
  15. #define _HW_PHB_C_
  16. #include "device_table.h"
  17. #include "hw_phb.h"
  18. #include "corefile.h"
  19. #ifdef HAVE_STDLIB_H
  20. #include <stdlib.h>
  21. #endif
  22. #include <ctype.h>
  23. /* DEVICE
  24. phb - PCI Host Bridge
  25. DESCRIPTION
  26. PHB implements a model of the PCI-host bridge described in the PPCP
  27. document.
  28. For bridge devices, Open Firmware specifies that the <<ranges>>
  29. property be used to specify the mapping of address spaces between a
  30. bridges parent and child busses. This PHB model configures itsself
  31. according to the information specified in its ranges property. The
  32. <<ranges>> property is described in detail in the Open Firmware
  33. documentation.
  34. For DMA transfers, any access to a PCI address space which falls
  35. outside of the mapped memory space is assumed to be a transfer
  36. intended for the parent bus.
  37. PROPERTIES
  38. ranges = <my-phys-addr> <parent-phys-addr> <my-size> ... (required)
  39. Define a number of mappings from the parent bus to one of this
  40. devices PCI busses. The exact format of the <<parent-phys-addr>>
  41. is parent bus dependant. The format of <<my-phys-addr>> is
  42. described in the Open Firmware PCI bindings document (note that the
  43. address must be non-relocatable).
  44. #address-cells = 3 (required)
  45. Number of cells used by an Open Firmware PCI address. This
  46. property must be defined before specifying the <<ranges>> property.
  47. #size-cells = 2 (required)
  48. Number of cells used by an Open Firmware PCI size. This property
  49. must be defined before specifying the <<ranges>> property.
  50. EXAMPLES
  51. Enable tracing:
  52. | $ psim \
  53. | -t phb-device \
  54. Since device tree entries that are specified on the command line
  55. are added before most of the device tree has been built it is often
  56. necessary to explictly add certain device properties and thus
  57. ensure they are already present in the device tree. For the
  58. <<phb>> one such property is parent busses <<#address-cells>>.
  59. | -o '/#address-cells 1' \
  60. Create the PHB remembering to include the cell size properties:
  61. | -o '/phb@0x80000000/#address-cells 3' \
  62. | -o '/phb@0x80000000/#size-cells 2' \
  63. Specify that the memory address range <<0x80000000>> to
  64. <<0x8fffffff>> should map directly onto the PCI memory address
  65. space while the processor address range <<0xc0000000>> to
  66. <<0xc000ffff>> should map onto the PCI I/O address range starting
  67. at location zero:
  68. | -o '/phb@0x80000000/ranges \
  69. | nm0,0,0,80000000 0x80000000 0x10000000 \
  70. | ni0,0,0,0 0xc0000000 0x10000' \
  71. Insert a 4k <<nvram>> into slot zero of the PCI bus. Have it
  72. directly accessible in both the I/O (address <<0x100>>) and memory
  73. (address 0x80001000) spaces:
  74. | -o '/phb@0x80000000/nvram@0/assigned-addresses \
  75. | nm0,0,10,80001000 4096 \
  76. | ni0,0,14,100 4096'
  77. | -o '/phb@0x80000000/nvram@0/reg \
  78. | 0 0 \
  79. | i0,0,14,0 4096'
  80. | -o '/phb@0x80000000/nvram@0/alternate-reg \
  81. | 0 0 \
  82. | m0,0,10,0 4096'
  83. The <<assigned-address>> property corresponding to what (if it were
  84. implemented) be found in the config base registers while the
  85. <<reg>> and <<alternative-reg>> properties indicating the location
  86. of registers within each address space.
  87. Of the possible addresses, only the non-relocatable versions are
  88. used when attaching the device to the bus.
  89. BUGS
  90. The implementation of the PCI configuration space is left as an
  91. exercise for the reader. Such a restriction should only impact on
  92. systems wanting to dynamically configure devices on the PCI bus.
  93. The <<CHRP>> document specfies additional (optional) functionality
  94. of the primary PHB. The implementation of such functionality is
  95. left as an exercise for the reader.
  96. The Open Firmware PCI bus bindings document (rev 1.6 and 2.0) is
  97. unclear on the value of the "ss" bits for a 64bit memory address.
  98. The correct value, as used by this module, is 0b11.
  99. The Open Firmware PCI bus bindings document (rev 1.6) suggests that
  100. the register field of non-relocatable PCI address should be zero.
  101. Unfortunatly, PCI addresses specified in the <<assigned-addresses>>
  102. property must be both non-relocatable and have non-zero register
  103. fields.
  104. The unit-decode method is not inserting a bus number into any
  105. address that it decodes. Instead the bus-number is left as zero.
  106. Support for aliased memory and I/O addresses is left as an exercise
  107. for the reader.
  108. Support for interrupt-ack and special cycles are left as an
  109. exercise for the reader. One issue to consider when attempting
  110. this exercise is how to specify the address of the int-ack and
  111. special cycle register. Hint: <</8259-interrupt-ackowledge>> is
  112. the wrong answer.
  113. Children of this node can only use the client callback interface
  114. when attaching themselves to the <<phb>>.
  115. REFERENCES
  116. http://playground.sun.com/1275/home.html#OFDbusPCI
  117. */
  118. typedef struct _phb_space {
  119. core *map;
  120. core_map *readable;
  121. core_map *writeable;
  122. unsigned_word parent_base;
  123. int parent_space;
  124. unsigned_word my_base;
  125. int my_space;
  126. unsigned size;
  127. const char *name;
  128. } phb_space;
  129. typedef struct _hw_phb_device {
  130. phb_space space[nr_hw_phb_spaces];
  131. } hw_phb_device;
  132. static const char *
  133. hw_phb_decode_name(hw_phb_decode level)
  134. {
  135. switch (level) {
  136. case hw_phb_normal_decode: return "normal";
  137. case hw_phb_subtractive_decode: return "subtractive";
  138. case hw_phb_master_abort_decode: return "master-abort";
  139. default: return "invalid decode";
  140. }
  141. }
  142. static void
  143. hw_phb_init_address(device *me)
  144. {
  145. hw_phb_device *phb = device_data(me);
  146. /* check some basic properties */
  147. if (device_nr_address_cells(me) != 3)
  148. device_error(me, "incorrect #address-cells");
  149. if (device_nr_size_cells(me) != 2)
  150. device_error(me, "incorrect #size-cells");
  151. /* (re) initialize each PCI space */
  152. {
  153. hw_phb_spaces space_nr;
  154. for (space_nr = 0; space_nr < nr_hw_phb_spaces; space_nr++) {
  155. phb_space *pci_space = &phb->space[space_nr];
  156. core_init(pci_space->map);
  157. pci_space->size = 0;
  158. }
  159. }
  160. /* decode each of the ranges properties entering the information
  161. into the space table */
  162. {
  163. range_property_spec range;
  164. int ranges_entry;
  165. for (ranges_entry = 0;
  166. device_find_range_array_property(me, "ranges", ranges_entry,
  167. &range);
  168. ranges_entry++) {
  169. int my_attach_space;
  170. unsigned_word my_attach_address;
  171. int parent_attach_space;
  172. unsigned_word parent_attach_address;
  173. unsigned size;
  174. phb_space *pci_space;
  175. /* convert the addresses into something meaningful */
  176. device_address_to_attach_address(me, &range.child_address,
  177. &my_attach_space,
  178. &my_attach_address,
  179. me);
  180. device_address_to_attach_address(device_parent(me),
  181. &range.parent_address,
  182. &parent_attach_space,
  183. &parent_attach_address,
  184. me);
  185. device_size_to_attach_size(me, &range.size, &size, me);
  186. if (my_attach_space < 0 || my_attach_space >= nr_hw_phb_spaces)
  187. device_error(me, "ranges property contains an invalid address space");
  188. pci_space = &phb->space[my_attach_space];
  189. if (pci_space->size != 0)
  190. device_error(me, "ranges property contains duplicate mappings for %s address space",
  191. pci_space->name);
  192. pci_space->parent_base = parent_attach_address;
  193. pci_space->parent_space = parent_attach_space;
  194. pci_space->my_base = my_attach_address;
  195. pci_space->my_space = my_attach_space;
  196. pci_space->size = size;
  197. device_attach_address(device_parent(me),
  198. attach_callback,
  199. parent_attach_space, parent_attach_address, size,
  200. access_read_write_exec,
  201. me);
  202. DTRACE(phb, ("map %d:0x%lx to %s:0x%lx (0x%lx bytes)\n",
  203. (int)parent_attach_space,
  204. (unsigned long)parent_attach_address,
  205. pci_space->name,
  206. (unsigned long)my_attach_address,
  207. (unsigned long)size));
  208. }
  209. if (ranges_entry == 0) {
  210. device_error(me, "Missing or empty ranges property");
  211. }
  212. }
  213. }
  214. static void
  215. hw_phb_attach_address(device *me,
  216. attach_type type,
  217. int space,
  218. unsigned_word addr,
  219. unsigned nr_bytes,
  220. access_type access,
  221. device *client) /*callback/default*/
  222. {
  223. hw_phb_device *phb = device_data(me);
  224. phb_space *pci_space;
  225. /* sanity checks */
  226. if (space < 0 || space >= nr_hw_phb_spaces)
  227. device_error(me, "attach space (%d) specified by %s invalid",
  228. space, device_path(client));
  229. pci_space = &phb->space[space];
  230. if (addr + nr_bytes > pci_space->my_base + pci_space->size
  231. || addr < pci_space->my_base)
  232. device_error(me, "attach addr (0x%lx) specified by %s outside of bus address range",
  233. (unsigned long)addr, device_path(client));
  234. if (type != hw_phb_normal_decode
  235. && type != hw_phb_subtractive_decode)
  236. device_error(me, "attach type (%d) specified by %s invalid",
  237. type, device_path(client));
  238. /* attach it to the relevent bus */
  239. DTRACE(phb, ("attach %s - %s %s:0x%lx (0x%lx bytes)\n",
  240. device_path(client),
  241. hw_phb_decode_name(type),
  242. pci_space->name,
  243. (unsigned long)addr,
  244. (unsigned long)nr_bytes));
  245. core_attach(pci_space->map,
  246. type,
  247. space,
  248. access,
  249. addr,
  250. nr_bytes,
  251. client);
  252. }
  253. /* Extract/set various fields from a PCI unit address.
  254. Note: only the least significant 32 bits of each cell is used.
  255. Note: for PPC MSB is 0 while for PCI it is 31. */
  256. /* relocatable bit n */
  257. static unsigned
  258. extract_n(const device_unit *address)
  259. {
  260. return EXTRACTED32(address->cells[0], 0, 0);
  261. }
  262. static void
  263. set_n(device_unit *address)
  264. {
  265. BLIT32(address->cells[0], 0, 1);
  266. }
  267. /* prefetchable bit p */
  268. static unsigned
  269. extract_p(const device_unit *address)
  270. {
  271. ASSERT(address->nr_cells == 3);
  272. return EXTRACTED32(address->cells[0], 1, 1);
  273. }
  274. static void
  275. set_p(device_unit *address)
  276. {
  277. BLIT32(address->cells[0], 1, 1);
  278. }
  279. /* aliased bit t */
  280. static unsigned
  281. extract_t(const device_unit *address)
  282. {
  283. ASSERT(address->nr_cells == 3);
  284. return EXTRACTED32(address->cells[0], 2, 2);
  285. }
  286. static void
  287. set_t(device_unit *address)
  288. {
  289. BLIT32(address->cells[0], 2, 1);
  290. }
  291. /* space code ss */
  292. typedef enum {
  293. ss_config_code = 0,
  294. ss_io_code = 1,
  295. ss_32bit_memory_code = 2,
  296. ss_64bit_memory_code = 3,
  297. } ss_type;
  298. static ss_type
  299. extract_ss(const device_unit *address)
  300. {
  301. ASSERT(address->nr_cells == 3);
  302. return EXTRACTED32(address->cells[0], 6, 7);
  303. }
  304. static void
  305. set_ss(device_unit *address, ss_type val)
  306. {
  307. MBLIT32(address->cells[0], 6, 7, val);
  308. }
  309. /* bus number bbbbbbbb */
  310. #if 0
  311. static unsigned
  312. extract_bbbbbbbb(const device_unit *address)
  313. {
  314. ASSERT(address->nr_cells == 3);
  315. return EXTRACTED32(address->cells[0], 8, 15);
  316. }
  317. #endif
  318. #if 0
  319. static void
  320. set_bbbbbbbb(device_unit *address, unsigned val)
  321. {
  322. MBLIT32(address->cells[0], 8, 15, val);
  323. }
  324. #endif
  325. /* device number ddddd */
  326. static unsigned
  327. extract_ddddd(const device_unit *address)
  328. {
  329. ASSERT(address->nr_cells == 3);
  330. return EXTRACTED32(address->cells[0], 16, 20);
  331. }
  332. static void
  333. set_ddddd(device_unit *address, unsigned val)
  334. {
  335. MBLIT32(address->cells[0], 16, 20, val);
  336. }
  337. /* function number fff */
  338. static unsigned
  339. extract_fff(const device_unit *address)
  340. {
  341. ASSERT(address->nr_cells == 3);
  342. return EXTRACTED32(address->cells[0], 21, 23);
  343. }
  344. static void
  345. set_fff(device_unit *address, unsigned val)
  346. {
  347. MBLIT32(address->cells[0], 21, 23, val);
  348. }
  349. /* register number rrrrrrrr */
  350. static unsigned
  351. extract_rrrrrrrr(const device_unit *address)
  352. {
  353. ASSERT(address->nr_cells == 3);
  354. return EXTRACTED32(address->cells[0], 24, 31);
  355. }
  356. static void
  357. set_rrrrrrrr(device_unit *address, unsigned val)
  358. {
  359. MBLIT32(address->cells[0], 24, 31, val);
  360. }
  361. /* MSW of 64bit address hh..hh */
  362. static unsigned
  363. extract_hh_hh(const device_unit *address)
  364. {
  365. ASSERT(address->nr_cells == 3);
  366. return address->cells[1];
  367. }
  368. static void
  369. set_hh_hh(device_unit *address, unsigned val)
  370. {
  371. address->cells[2] = val;
  372. }
  373. /* LSW of 64bit address ll..ll */
  374. static unsigned
  375. extract_ll_ll(const device_unit *address)
  376. {
  377. ASSERT(address->nr_cells == 3);
  378. return address->cells[2];
  379. }
  380. static void
  381. set_ll_ll(device_unit *address, unsigned val)
  382. {
  383. address->cells[2] = val;
  384. }
  385. /* Convert PCI textual bus address into a device unit */
  386. static int
  387. hw_phb_unit_decode(device *me,
  388. const char *unit,
  389. device_unit *address)
  390. {
  391. char *end = NULL;
  392. const char *chp = unit;
  393. unsigned long val;
  394. if (device_nr_address_cells(me) != 3)
  395. device_error(me, "PCI bus should have #address-cells == 3");
  396. memset(address, 0, sizeof(*address));
  397. if (unit == NULL)
  398. return 0;
  399. address->nr_cells = 3;
  400. if (isxdigit(*chp)) {
  401. set_ss(address, ss_config_code);
  402. }
  403. else {
  404. /* non-relocatable? */
  405. if (*chp == 'n') {
  406. set_n(address);
  407. chp++;
  408. }
  409. /* address-space? */
  410. if (*chp == 'i') {
  411. set_ss(address, ss_io_code);
  412. chp++;
  413. }
  414. else if (*chp == 'm') {
  415. set_ss(address, ss_32bit_memory_code);
  416. chp++;
  417. }
  418. else if (*chp == 'x') {
  419. set_ss(address, ss_64bit_memory_code);
  420. chp++;
  421. }
  422. else
  423. device_error(me, "Problem parsing PCI address %s", unit);
  424. /* possible alias */
  425. if (*chp == 't') {
  426. if (extract_ss(address) == ss_64bit_memory_code)
  427. device_error(me, "Invalid alias bit in PCI address %s", unit);
  428. set_t(address);
  429. chp++;
  430. }
  431. /* possible p */
  432. if (*chp == 'p') {
  433. if (extract_ss(address) != ss_32bit_memory_code)
  434. device_error(me, "Invalid prefetchable bit (p) in PCI address %s",
  435. unit);
  436. set_p(address);
  437. chp++;
  438. }
  439. }
  440. /* required DD */
  441. if (!isxdigit(*chp))
  442. device_error(me, "Missing device number in PCI address %s", unit);
  443. val = strtoul(chp, &end, 16);
  444. if (chp == end)
  445. device_error(me, "Problem parsing device number in PCI address %s", unit);
  446. if ((val & 0x1f) != val)
  447. device_error(me, "Device number (0x%lx) out of range (0..0x1f) in PCI address %s",
  448. val, unit);
  449. set_ddddd(address, val);
  450. chp = end;
  451. /* For config space, the F is optional */
  452. if (extract_ss(address) == ss_config_code
  453. && (isspace(*chp) || *chp == '\0'))
  454. return chp - unit;
  455. /* function number F */
  456. if (*chp != ',')
  457. device_error(me, "Missing function number in PCI address %s", unit);
  458. chp++;
  459. val = strtoul(chp, &end, 10);
  460. if (chp == end)
  461. device_error(me, "Problem parsing function number in PCI address %s",
  462. unit);
  463. if ((val & 7) != val)
  464. device_error(me, "Function number (%ld) out of range (0..7) in PCI address %s",
  465. (long)val, unit);
  466. set_fff(address, val);
  467. chp = end;
  468. /* for config space, must be end */
  469. if (extract_ss(address) == ss_config_code) {
  470. if (!isspace(*chp) && *chp != '\0')
  471. device_error(me, "Problem parsing PCI config address %s",
  472. unit);
  473. return chp - unit;
  474. }
  475. /* register number RR */
  476. if (*chp != ',')
  477. device_error(me, "Missing register number in PCI address %s", unit);
  478. chp++;
  479. val = strtoul(chp, &end, 16);
  480. if (chp == end)
  481. device_error(me, "Problem parsing register number in PCI address %s",
  482. unit);
  483. switch (extract_ss(address)) {
  484. case ss_io_code:
  485. #if 0
  486. if (extract_n(address) && val != 0)
  487. device_error(me, "non-relocatable I/O register must be zero in PCI address %s", unit);
  488. else if (!extract_n(address)
  489. && val != 0x10 && val != 0x14 && val != 0x18
  490. && val != 0x1c && val != 0x20 && val != 0x24)
  491. device_error(me, "I/O register invalid in PCI address %s", unit);
  492. #endif
  493. break;
  494. case ss_32bit_memory_code:
  495. #if 0
  496. if (extract_n(address) && val != 0)
  497. device_error(me, "non-relocatable memory register must be zero in PCI address %s", unit);
  498. else if (!extract_n(address)
  499. && val != 0x10 && val != 0x14 && val != 0x18
  500. && val != 0x1c && val != 0x20 && val != 0x24 && val != 0x30)
  501. device_error(me, "I/O register (0x%lx) invalid in PCI address %s",
  502. val, unit);
  503. #endif
  504. break;
  505. case ss_64bit_memory_code:
  506. if (extract_n(address) && val != 0)
  507. device_error(me, "non-relocatable 32bit memory register must be zero in PCI address %s", unit);
  508. else if (!extract_n(address)
  509. && val != 0x10 && val != 0x18 && val != 0x20)
  510. device_error(me, "Register number (0x%lx) invalid in 64bit PCI address %s",
  511. val, unit);
  512. case ss_config_code:
  513. device_error(me, "internal error");
  514. }
  515. if ((val & 0xff) != val)
  516. device_error(me, "Register number (0x%lx) out of range (0..0xff) in PCI address %s",
  517. val, unit);
  518. set_rrrrrrrr(address, val);
  519. chp = end;
  520. /* address */
  521. if (*chp != ',')
  522. device_error(me, "Missing address in PCI address %s", unit);
  523. chp++;
  524. switch (extract_ss(address)) {
  525. case ss_io_code:
  526. case ss_32bit_memory_code:
  527. val = strtoul(chp, &end, 16);
  528. if (chp == end)
  529. device_error(me, "Problem parsing address in PCI address %s", unit);
  530. switch (extract_ss(address)) {
  531. case ss_io_code:
  532. if (extract_n(address) && extract_t(address)
  533. && (val & 1024) != val)
  534. device_error(me, "10bit aliased non-relocatable address (0x%lx) out of range in PCI address %s",
  535. val, unit);
  536. if (!extract_n(address) && extract_t(address)
  537. && (val & 0xffff) != val)
  538. device_error(me, "64k relocatable address (0x%lx) out of range in PCI address %s",
  539. val, unit);
  540. break;
  541. case ss_32bit_memory_code:
  542. if (extract_t(address) && (val & 0xfffff) != val)
  543. device_error(me, "1mb memory address (0x%lx) out of range in PCI address %s",
  544. val, unit);
  545. if (!extract_t(address) && (val & 0xffffffff) != val)
  546. device_error(me, "32bit memory address (0x%lx) out of range in PCI address %s",
  547. val, unit);
  548. break;
  549. case ss_64bit_memory_code:
  550. case ss_config_code:
  551. device_error(me, "internal error");
  552. }
  553. set_ll_ll(address, val);
  554. chp = end;
  555. break;
  556. case ss_64bit_memory_code:
  557. device_error(me, "64bit addresses unimplemented");
  558. set_hh_hh(address, val);
  559. set_ll_ll(address, val);
  560. break;
  561. case ss_config_code:
  562. device_error(me, "internal error");
  563. break;
  564. }
  565. /* finished? */
  566. if (!isspace(*chp) && *chp != '\0')
  567. device_error(me, "Problem parsing PCI address %s", unit);
  568. return chp - unit;
  569. }
  570. /* Convert PCI device unit into its corresponding textual
  571. representation */
  572. static int
  573. hw_phb_unit_encode(device *me,
  574. const device_unit *unit_address,
  575. char *buf,
  576. int sizeof_buf)
  577. {
  578. if (unit_address->nr_cells != 3)
  579. device_error(me, "Incorrect number of cells in PCI unit address");
  580. if (device_nr_address_cells(me) != 3)
  581. device_error(me, "PCI bus should have #address-cells == 3");
  582. if (extract_ss(unit_address) == ss_config_code
  583. && extract_fff(unit_address) == 0
  584. && extract_rrrrrrrr(unit_address) == 0
  585. && extract_hh_hh(unit_address) == 0
  586. && extract_ll_ll(unit_address) == 0) {
  587. /* DD - Configuration Space address */
  588. sprintf(buf, "%x",
  589. extract_ddddd(unit_address));
  590. }
  591. else if (extract_ss(unit_address) == ss_config_code
  592. && extract_fff(unit_address) != 0
  593. && extract_rrrrrrrr(unit_address) == 0
  594. && extract_hh_hh(unit_address) == 0
  595. && extract_ll_ll(unit_address) == 0) {
  596. /* DD,F - Configuration Space */
  597. sprintf(buf, "%x,%d",
  598. extract_ddddd(unit_address),
  599. extract_fff(unit_address));
  600. }
  601. else if (extract_ss(unit_address) == ss_io_code
  602. && extract_hh_hh(unit_address) == 0) {
  603. /* [n]i[t]DD,F,RR,NNNNNNNN - 32bit I/O space */
  604. sprintf(buf, "%si%s%x,%d,%x,%x",
  605. extract_n(unit_address) ? "n" : "",
  606. extract_t(unit_address) ? "t" : "",
  607. extract_ddddd(unit_address),
  608. extract_fff(unit_address),
  609. extract_rrrrrrrr(unit_address),
  610. extract_ll_ll(unit_address));
  611. }
  612. else if (extract_ss(unit_address) == ss_32bit_memory_code
  613. && extract_hh_hh(unit_address) == 0) {
  614. /* [n]m[t][p]DD,F,RR,NNNNNNNN - 32bit memory space */
  615. sprintf(buf, "%sm%s%s%x,%d,%x,%x",
  616. extract_n(unit_address) ? "n" : "",
  617. extract_t(unit_address) ? "t" : "",
  618. extract_p(unit_address) ? "p" : "",
  619. extract_ddddd(unit_address),
  620. extract_fff(unit_address),
  621. extract_rrrrrrrr(unit_address),
  622. extract_ll_ll(unit_address));
  623. }
  624. else if (extract_ss(unit_address) == ss_32bit_memory_code) {
  625. /* [n]x[p]DD,F,RR,NNNNNNNNNNNNNNNN - 64bit memory space */
  626. sprintf(buf, "%sx%s%x,%d,%x,%x%08x",
  627. extract_n(unit_address) ? "n" : "",
  628. extract_p(unit_address) ? "p" : "",
  629. extract_ddddd(unit_address),
  630. extract_fff(unit_address),
  631. extract_rrrrrrrr(unit_address),
  632. extract_hh_hh(unit_address),
  633. extract_ll_ll(unit_address));
  634. }
  635. else {
  636. device_error(me, "Invalid PCI unit address 0x%08lx 0x%08lx 0x%08lx",
  637. (unsigned long)unit_address->cells[0],
  638. (unsigned long)unit_address->cells[1],
  639. (unsigned long)unit_address->cells[2]);
  640. }
  641. if (strlen(buf) > sizeof_buf)
  642. error("buffer overflow");
  643. return strlen(buf);
  644. }
  645. static int
  646. hw_phb_address_to_attach_address(device *me,
  647. const device_unit *address,
  648. int *attach_space,
  649. unsigned_word *attach_address,
  650. device *client)
  651. {
  652. if (address->nr_cells != 3)
  653. device_error(me, "attach address has incorrect number of cells");
  654. if (address->cells[1] != 0)
  655. device_error(me, "64bit attach address unsupported");
  656. /* directly decode the address/space */
  657. *attach_address = address->cells[2];
  658. switch (extract_ss(address)) {
  659. case ss_config_code:
  660. *attach_space = hw_phb_config_space;
  661. break;
  662. case ss_io_code:
  663. *attach_space = hw_phb_io_space;
  664. break;
  665. case ss_32bit_memory_code:
  666. case ss_64bit_memory_code:
  667. *attach_space = hw_phb_memory_space;
  668. break;
  669. }
  670. /* if non-relocatable finished */
  671. if (extract_n(address))
  672. return 1;
  673. /* make memory and I/O addresses absolute */
  674. if (*attach_space == hw_phb_io_space
  675. || *attach_space == hw_phb_memory_space) {
  676. int reg_nr;
  677. reg_property_spec assigned;
  678. if (extract_ss(address) == ss_64bit_memory_code)
  679. device_error(me, "64bit memory address not unsuported");
  680. for (reg_nr = 0;
  681. device_find_reg_array_property(client, "assigned-addresses", reg_nr,
  682. &assigned);
  683. reg_nr++) {
  684. if (!extract_n(&assigned.address)
  685. || extract_rrrrrrrr(&assigned.address) == 0)
  686. device_error(me, "client %s has invalid assigned-address property",
  687. device_path(client));
  688. if (extract_rrrrrrrr(address) == extract_rrrrrrrr(&assigned.address)) {
  689. /* corresponding base register */
  690. if (extract_ss(address) != extract_ss(&assigned.address))
  691. device_error(me, "client %s has conflicting types for base register 0x%lx",
  692. device_path(client),
  693. (unsigned long)extract_rrrrrrrr(address));
  694. *attach_address += assigned.address.cells[2];
  695. return 0;
  696. }
  697. }
  698. device_error(me, "client %s missing base address register 0x%lx in assigned-addresses property",
  699. device_path(client),
  700. (unsigned long)extract_rrrrrrrr(address));
  701. }
  702. return 0;
  703. }
  704. static int
  705. hw_phb_size_to_attach_size(device *me,
  706. const device_unit *size,
  707. unsigned *nr_bytes,
  708. device *client)
  709. {
  710. if (size->nr_cells != 2)
  711. device_error(me, "size has incorrect number of cells");
  712. if (size->cells[0] != 0)
  713. device_error(me, "64bit size unsupported");
  714. *nr_bytes = size->cells[1];
  715. return size->cells[1];
  716. }
  717. static const phb_space *
  718. find_phb_space(hw_phb_device *phb,
  719. unsigned_word addr,
  720. unsigned nr_bytes)
  721. {
  722. hw_phb_spaces space;
  723. /* find the space that matches the address */
  724. for (space = 0; space < nr_hw_phb_spaces; space++) {
  725. phb_space *pci_space = &phb->space[space];
  726. if (addr >= pci_space->parent_base
  727. && (addr + nr_bytes) <= (pci_space->parent_base + pci_space->size)) {
  728. return pci_space;
  729. }
  730. }
  731. return NULL;
  732. }
  733. static unsigned_word
  734. map_phb_addr(const phb_space *space,
  735. unsigned_word addr)
  736. {
  737. return addr - space->parent_base + space->my_base;
  738. }
  739. static unsigned
  740. hw_phb_io_read_buffer(device *me,
  741. void *dest,
  742. int space,
  743. unsigned_word addr,
  744. unsigned nr_bytes,
  745. cpu *processor,
  746. unsigned_word cia)
  747. {
  748. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  749. const phb_space *pci_space = find_phb_space(phb, addr, nr_bytes);
  750. unsigned_word bus_addr;
  751. if (pci_space == NULL)
  752. return 0;
  753. bus_addr = map_phb_addr(pci_space, addr);
  754. DTRACE(phb, ("io read - %d:0x%lx -> %s:0x%lx (%u bytes)\n",
  755. space, (unsigned long)addr, pci_space->name, (unsigned long)bus_addr,
  756. nr_bytes));
  757. return core_map_read_buffer(pci_space->readable,
  758. dest, bus_addr, nr_bytes);
  759. }
  760. static unsigned
  761. hw_phb_io_write_buffer(device *me,
  762. const void *source,
  763. int space,
  764. unsigned_word addr,
  765. unsigned nr_bytes,
  766. cpu *processor,
  767. unsigned_word cia)
  768. {
  769. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  770. const phb_space *pci_space = find_phb_space(phb, addr, nr_bytes);
  771. unsigned_word bus_addr;
  772. if (pci_space == NULL)
  773. return 0;
  774. bus_addr = map_phb_addr(pci_space, addr);
  775. DTRACE(phb, ("io write - %d:0x%lx -> %s:0x%lx (%u bytes)\n",
  776. space, (unsigned long)addr, pci_space->name, (unsigned long)bus_addr,
  777. nr_bytes));
  778. return core_map_write_buffer(pci_space->writeable, source,
  779. bus_addr, nr_bytes);
  780. }
  781. static unsigned
  782. hw_phb_dma_read_buffer(device *me,
  783. void *dest,
  784. int space,
  785. unsigned_word addr,
  786. unsigned nr_bytes)
  787. {
  788. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  789. const phb_space *pci_space;
  790. /* find the space */
  791. if (space != hw_phb_memory_space)
  792. device_error(me, "invalid dma address space %d", space);
  793. pci_space = &phb->space[space];
  794. /* check out the address */
  795. if ((addr >= pci_space->my_base
  796. && addr <= pci_space->my_base + pci_space->size)
  797. || (addr + nr_bytes >= pci_space->my_base
  798. && addr + nr_bytes <= pci_space->my_base + pci_space->size))
  799. device_error(me, "Do not support DMA into own bus");
  800. /* do it */
  801. DTRACE(phb, ("dma read - %s:0x%lx (%d bytes)\n",
  802. pci_space->name, addr, nr_bytes));
  803. return device_dma_read_buffer(device_parent(me),
  804. dest, pci_space->parent_space,
  805. addr, nr_bytes);
  806. }
  807. static unsigned
  808. hw_phb_dma_write_buffer(device *me,
  809. const void *source,
  810. int space,
  811. unsigned_word addr,
  812. unsigned nr_bytes,
  813. int violate_read_only_section)
  814. {
  815. hw_phb_device *phb = (hw_phb_device*)device_data(me);
  816. const phb_space *pci_space;
  817. /* find the space */
  818. if (space != hw_phb_memory_space)
  819. device_error(me, "invalid dma address space %d", space);
  820. pci_space = &phb->space[space];
  821. /* check out the address */
  822. if ((addr >= pci_space->my_base
  823. && addr <= pci_space->my_base + pci_space->size)
  824. || (addr + nr_bytes >= pci_space->my_base
  825. && addr + nr_bytes <= pci_space->my_base + pci_space->size))
  826. device_error(me, "Do not support DMA into own bus");
  827. /* do it */
  828. DTRACE(phb, ("dma write - %s:0x%lx (%d bytes)\n",
  829. pci_space->name, addr, nr_bytes));
  830. return device_dma_write_buffer(device_parent(me),
  831. source, pci_space->parent_space,
  832. addr, nr_bytes,
  833. violate_read_only_section);
  834. }
  835. static device_callbacks const hw_phb_callbacks = {
  836. { hw_phb_init_address, },
  837. { hw_phb_attach_address, },
  838. { hw_phb_io_read_buffer, hw_phb_io_write_buffer },
  839. { hw_phb_dma_read_buffer, hw_phb_dma_write_buffer },
  840. { NULL, }, /* interrupt */
  841. { hw_phb_unit_decode,
  842. hw_phb_unit_encode,
  843. hw_phb_address_to_attach_address,
  844. hw_phb_size_to_attach_size }
  845. };
  846. static void *
  847. hw_phb_create(const char *name,
  848. const device_unit *unit_address,
  849. const char *args)
  850. {
  851. /* create the descriptor */
  852. hw_phb_device *phb = ZALLOC(hw_phb_device);
  853. /* create the core maps now */
  854. hw_phb_spaces space_nr;
  855. for (space_nr = 0; space_nr < nr_hw_phb_spaces; space_nr++) {
  856. phb_space *pci_space = &phb->space[space_nr];
  857. pci_space->map = core_create();
  858. pci_space->readable = core_readable(pci_space->map);
  859. pci_space->writeable = core_writeable(pci_space->map);
  860. switch (space_nr) {
  861. case hw_phb_memory_space:
  862. pci_space->name = "memory";
  863. break;
  864. case hw_phb_io_space:
  865. pci_space->name = "I/O";
  866. break;
  867. case hw_phb_config_space:
  868. pci_space->name = "config";
  869. break;
  870. case hw_phb_special_space:
  871. pci_space->name = "special";
  872. break;
  873. default:
  874. error ("internal error");
  875. break;
  876. }
  877. }
  878. return phb;
  879. }
  880. const device_descriptor hw_phb_device_descriptor[] = {
  881. { "phb", hw_phb_create, &hw_phb_callbacks },
  882. { "pci", NULL, &hw_phb_callbacks },
  883. { NULL, },
  884. };
  885. #endif /* _HW_PHB_ */