arbiter.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Memory arbiter functions. Allocates bandwith through the
  3. * arbiter and sets up arbiter breakpoints.
  4. *
  5. * The algorithm first assigns slots to the clients that has specified
  6. * bandwith (e.g. ethernet) and then the remaining slots are divided
  7. * on all the active clients.
  8. *
  9. * Copyright (c) 2004, 2005 Axis Communications AB.
  10. */
  11. #include <linux/config.h>
  12. #include <asm/arch/hwregs/reg_map.h>
  13. #include <asm/arch/hwregs/reg_rdwr.h>
  14. #include <asm/arch/hwregs/marb_defs.h>
  15. #include <asm/arch/arbiter.h>
  16. #include <asm/arch/hwregs/intr_vect.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/spinlock.h>
  21. #include <asm/io.h>
  22. struct crisv32_watch_entry
  23. {
  24. unsigned long instance;
  25. watch_callback* cb;
  26. unsigned long start;
  27. unsigned long end;
  28. int used;
  29. };
  30. #define NUMBER_OF_BP 4
  31. #define NBR_OF_CLIENTS 14
  32. #define NBR_OF_SLOTS 64
  33. #define SDRAM_BANDWIDTH 100000000 /* Some kind of expected value */
  34. #define INTMEM_BANDWIDTH 400000000
  35. #define NBR_OF_REGIONS 2
  36. static struct crisv32_watch_entry watches[NUMBER_OF_BP] =
  37. {
  38. {regi_marb_bp0},
  39. {regi_marb_bp1},
  40. {regi_marb_bp2},
  41. {regi_marb_bp3}
  42. };
  43. static int requested_slots[NBR_OF_REGIONS][NBR_OF_CLIENTS];
  44. static int active_clients[NBR_OF_REGIONS][NBR_OF_CLIENTS];
  45. static int max_bandwidth[NBR_OF_REGIONS] = {SDRAM_BANDWIDTH, INTMEM_BANDWIDTH};
  46. DEFINE_SPINLOCK(arbiter_lock);
  47. static irqreturn_t
  48. crisv32_arbiter_irq(int irq, void* dev_id, struct pt_regs* regs);
  49. static void crisv32_arbiter_config(int region)
  50. {
  51. int slot;
  52. int client;
  53. int interval = 0;
  54. int val[NBR_OF_SLOTS];
  55. for (slot = 0; slot < NBR_OF_SLOTS; slot++)
  56. val[slot] = NBR_OF_CLIENTS + 1;
  57. for (client = 0; client < NBR_OF_CLIENTS; client++)
  58. {
  59. int pos;
  60. if (!requested_slots[region][client])
  61. continue;
  62. interval = NBR_OF_SLOTS / requested_slots[region][client];
  63. pos = 0;
  64. while (pos < NBR_OF_SLOTS)
  65. {
  66. if (val[pos] != NBR_OF_CLIENTS + 1)
  67. pos++;
  68. else
  69. {
  70. val[pos] = client;
  71. pos += interval;
  72. }
  73. }
  74. }
  75. client = 0;
  76. for (slot = 0; slot < NBR_OF_SLOTS; slot++)
  77. {
  78. if (val[slot] == NBR_OF_CLIENTS + 1)
  79. {
  80. int first = client;
  81. while(!active_clients[region][client]) {
  82. client = (client + 1) % NBR_OF_CLIENTS;
  83. if (client == first)
  84. break;
  85. }
  86. val[slot] = client;
  87. client = (client + 1) % NBR_OF_CLIENTS;
  88. }
  89. if (region == EXT_REGION)
  90. REG_WR_INT_VECT(marb, regi_marb, rw_ext_slots, slot, val[slot]);
  91. else if (region == INT_REGION)
  92. REG_WR_INT_VECT(marb, regi_marb, rw_int_slots, slot, val[slot]);
  93. }
  94. }
  95. extern char _stext, _etext;
  96. static void crisv32_arbiter_init(void)
  97. {
  98. static int initialized = 0;
  99. if (initialized)
  100. return;
  101. initialized = 1;
  102. /* CPU caches are active. */
  103. active_clients[EXT_REGION][10] = active_clients[EXT_REGION][11] = 1;
  104. crisv32_arbiter_config(EXT_REGION);
  105. crisv32_arbiter_config(INT_REGION);
  106. if (request_irq(MEMARB_INTR_VECT, crisv32_arbiter_irq, SA_INTERRUPT,
  107. "arbiter", NULL))
  108. printk(KERN_ERR "Couldn't allocate arbiter IRQ\n");
  109. #ifndef CONFIG_ETRAX_KGDB
  110. /* Global watch for writes to kernel text segment. */
  111. crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
  112. arbiter_all_clients, arbiter_all_write, NULL);
  113. #endif
  114. }
  115. int crisv32_arbiter_allocate_bandwith(int client, int region,
  116. unsigned long bandwidth)
  117. {
  118. int i;
  119. int total_assigned = 0;
  120. int total_clients = 0;
  121. int req;
  122. crisv32_arbiter_init();
  123. for (i = 0; i < NBR_OF_CLIENTS; i++)
  124. {
  125. total_assigned += requested_slots[region][i];
  126. total_clients += active_clients[region][i];
  127. }
  128. req = NBR_OF_SLOTS / (max_bandwidth[region] / bandwidth);
  129. if (total_assigned + total_clients + req + 1 > NBR_OF_SLOTS)
  130. return -ENOMEM;
  131. active_clients[region][client] = 1;
  132. requested_slots[region][client] = req;
  133. crisv32_arbiter_config(region);
  134. return 0;
  135. }
  136. int crisv32_arbiter_watch(unsigned long start, unsigned long size,
  137. unsigned long clients, unsigned long accesses,
  138. watch_callback* cb)
  139. {
  140. int i;
  141. crisv32_arbiter_init();
  142. if (start > 0x80000000) {
  143. printk("Arbiter: %lX doesn't look like a physical address", start);
  144. return -EFAULT;
  145. }
  146. spin_lock(&arbiter_lock);
  147. for (i = 0; i < NUMBER_OF_BP; i++) {
  148. if (!watches[i].used) {
  149. reg_marb_rw_intr_mask intr_mask = REG_RD(marb, regi_marb, rw_intr_mask);
  150. watches[i].used = 1;
  151. watches[i].start = start;
  152. watches[i].end = start + size;
  153. watches[i].cb = cb;
  154. REG_WR_INT(marb_bp, watches[i].instance, rw_first_addr, watches[i].start);
  155. REG_WR_INT(marb_bp, watches[i].instance, rw_last_addr, watches[i].end);
  156. REG_WR_INT(marb_bp, watches[i].instance, rw_op, accesses);
  157. REG_WR_INT(marb_bp, watches[i].instance, rw_clients, clients);
  158. if (i == 0)
  159. intr_mask.bp0 = regk_marb_yes;
  160. else if (i == 1)
  161. intr_mask.bp1 = regk_marb_yes;
  162. else if (i == 2)
  163. intr_mask.bp2 = regk_marb_yes;
  164. else if (i == 3)
  165. intr_mask.bp3 = regk_marb_yes;
  166. REG_WR(marb, regi_marb, rw_intr_mask, intr_mask);
  167. spin_unlock(&arbiter_lock);
  168. return i;
  169. }
  170. }
  171. spin_unlock(&arbiter_lock);
  172. return -ENOMEM;
  173. }
  174. int crisv32_arbiter_unwatch(int id)
  175. {
  176. reg_marb_rw_intr_mask intr_mask = REG_RD(marb, regi_marb, rw_intr_mask);
  177. crisv32_arbiter_init();
  178. spin_lock(&arbiter_lock);
  179. if ((id < 0) || (id >= NUMBER_OF_BP) || (!watches[id].used)) {
  180. spin_unlock(&arbiter_lock);
  181. return -EINVAL;
  182. }
  183. memset(&watches[id], 0, sizeof(struct crisv32_watch_entry));
  184. if (id == 0)
  185. intr_mask.bp0 = regk_marb_no;
  186. else if (id == 1)
  187. intr_mask.bp2 = regk_marb_no;
  188. else if (id == 2)
  189. intr_mask.bp2 = regk_marb_no;
  190. else if (id == 3)
  191. intr_mask.bp3 = regk_marb_no;
  192. REG_WR(marb, regi_marb, rw_intr_mask, intr_mask);
  193. spin_unlock(&arbiter_lock);
  194. return 0;
  195. }
  196. extern void show_registers(struct pt_regs *regs);
  197. static irqreturn_t
  198. crisv32_arbiter_irq(int irq, void* dev_id, struct pt_regs* regs)
  199. {
  200. reg_marb_r_masked_intr masked_intr = REG_RD(marb, regi_marb, r_masked_intr);
  201. reg_marb_bp_r_brk_clients r_clients;
  202. reg_marb_bp_r_brk_addr r_addr;
  203. reg_marb_bp_r_brk_op r_op;
  204. reg_marb_bp_r_brk_first_client r_first;
  205. reg_marb_bp_r_brk_size r_size;
  206. reg_marb_bp_rw_ack ack = {0};
  207. reg_marb_rw_ack_intr ack_intr = {.bp0=1,.bp1=1,.bp2=1,.bp3=1};
  208. struct crisv32_watch_entry* watch;
  209. if (masked_intr.bp0) {
  210. watch = &watches[0];
  211. ack_intr.bp0 = regk_marb_yes;
  212. } else if (masked_intr.bp1) {
  213. watch = &watches[1];
  214. ack_intr.bp1 = regk_marb_yes;
  215. } else if (masked_intr.bp2) {
  216. watch = &watches[2];
  217. ack_intr.bp2 = regk_marb_yes;
  218. } else if (masked_intr.bp3) {
  219. watch = &watches[3];
  220. ack_intr.bp3 = regk_marb_yes;
  221. } else {
  222. return IRQ_NONE;
  223. }
  224. /* Retrieve all useful information and print it. */
  225. r_clients = REG_RD(marb_bp, watch->instance, r_brk_clients);
  226. r_addr = REG_RD(marb_bp, watch->instance, r_brk_addr);
  227. r_op = REG_RD(marb_bp, watch->instance, r_brk_op);
  228. r_first = REG_RD(marb_bp, watch->instance, r_brk_first_client);
  229. r_size = REG_RD(marb_bp, watch->instance, r_brk_size);
  230. printk("Arbiter IRQ\n");
  231. printk("Clients %X addr %X op %X first %X size %X\n",
  232. REG_TYPE_CONV(int, reg_marb_bp_r_brk_clients, r_clients),
  233. REG_TYPE_CONV(int, reg_marb_bp_r_brk_addr, r_addr),
  234. REG_TYPE_CONV(int, reg_marb_bp_r_brk_op, r_op),
  235. REG_TYPE_CONV(int, reg_marb_bp_r_brk_first_client, r_first),
  236. REG_TYPE_CONV(int, reg_marb_bp_r_brk_size, r_size));
  237. REG_WR(marb_bp, watch->instance, rw_ack, ack);
  238. REG_WR(marb, regi_marb, rw_ack_intr, ack_intr);
  239. printk("IRQ occured at %lX\n", regs->erp);
  240. if (watch->cb)
  241. watch->cb();
  242. return IRQ_HANDLED;
  243. }