sched.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* sched.c - SPU scheduler.
  2. *
  3. * Copyright (C) IBM 2005
  4. * Author: Mark Nutter <mnutter@us.ibm.com>
  5. *
  6. * SPU scheduler, based on Linux thread priority. For now use
  7. * a simple "cooperative" yield model with no preemption. SPU
  8. * scheduling will eventually be preemptive: When a thread with
  9. * a higher static priority gets ready to run, then an active SPU
  10. * context will be preempted and returned to the waitq.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more 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. #undef DEBUG
  27. #include <linux/config.h>
  28. #include <linux/module.h>
  29. #include <linux/errno.h>
  30. #include <linux/sched.h>
  31. #include <linux/kernel.h>
  32. #include <linux/mm.h>
  33. #include <linux/completion.h>
  34. #include <linux/vmalloc.h>
  35. #include <linux/smp.h>
  36. #include <linux/smp_lock.h>
  37. #include <linux/stddef.h>
  38. #include <linux/unistd.h>
  39. #include <asm/io.h>
  40. #include <asm/mmu_context.h>
  41. #include <asm/spu.h>
  42. #include <asm/spu_csa.h>
  43. #include "spufs.h"
  44. #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
  45. struct spu_prio_array {
  46. atomic_t nr_blocked;
  47. unsigned long bitmap[SPU_BITMAP_SIZE];
  48. wait_queue_head_t waitq[MAX_PRIO];
  49. };
  50. /* spu_runqueue - This is the main runqueue data structure for SPUs. */
  51. struct spu_runqueue {
  52. struct semaphore sem;
  53. unsigned long nr_active;
  54. unsigned long nr_idle;
  55. unsigned long nr_switches;
  56. struct list_head active_list;
  57. struct list_head idle_list;
  58. struct spu_prio_array prio;
  59. };
  60. static struct spu_runqueue *spu_runqueues = NULL;
  61. static inline struct spu_runqueue *spu_rq(void)
  62. {
  63. /* Future: make this a per-NODE array,
  64. * and use cpu_to_node(smp_processor_id())
  65. */
  66. return spu_runqueues;
  67. }
  68. static inline struct spu *del_idle(struct spu_runqueue *rq)
  69. {
  70. struct spu *spu;
  71. BUG_ON(rq->nr_idle <= 0);
  72. BUG_ON(list_empty(&rq->idle_list));
  73. /* Future: Move SPU out of low-power SRI state. */
  74. spu = list_entry(rq->idle_list.next, struct spu, sched_list);
  75. list_del_init(&spu->sched_list);
  76. rq->nr_idle--;
  77. return spu;
  78. }
  79. static inline void del_active(struct spu_runqueue *rq, struct spu *spu)
  80. {
  81. BUG_ON(rq->nr_active <= 0);
  82. BUG_ON(list_empty(&rq->active_list));
  83. list_del_init(&spu->sched_list);
  84. rq->nr_active--;
  85. }
  86. static inline void add_idle(struct spu_runqueue *rq, struct spu *spu)
  87. {
  88. /* Future: Put SPU into low-power SRI state. */
  89. list_add_tail(&spu->sched_list, &rq->idle_list);
  90. rq->nr_idle++;
  91. }
  92. static inline void add_active(struct spu_runqueue *rq, struct spu *spu)
  93. {
  94. rq->nr_active++;
  95. rq->nr_switches++;
  96. list_add_tail(&spu->sched_list, &rq->active_list);
  97. }
  98. static void prio_wakeup(struct spu_runqueue *rq)
  99. {
  100. if (atomic_read(&rq->prio.nr_blocked) && rq->nr_idle) {
  101. int best = sched_find_first_bit(rq->prio.bitmap);
  102. if (best < MAX_PRIO) {
  103. wait_queue_head_t *wq = &rq->prio.waitq[best];
  104. wake_up_interruptible_nr(wq, 1);
  105. }
  106. }
  107. }
  108. static void prio_wait(struct spu_runqueue *rq, struct spu_context *ctx,
  109. u64 flags)
  110. {
  111. int prio = current->prio;
  112. wait_queue_head_t *wq = &rq->prio.waitq[prio];
  113. DEFINE_WAIT(wait);
  114. __set_bit(prio, rq->prio.bitmap);
  115. atomic_inc(&rq->prio.nr_blocked);
  116. prepare_to_wait_exclusive(wq, &wait, TASK_INTERRUPTIBLE);
  117. if (!signal_pending(current)) {
  118. up(&rq->sem);
  119. up_write(&ctx->state_sema);
  120. pr_debug("%s: pid=%d prio=%d\n", __FUNCTION__,
  121. current->pid, current->prio);
  122. schedule();
  123. down_write(&ctx->state_sema);
  124. down(&rq->sem);
  125. }
  126. finish_wait(wq, &wait);
  127. atomic_dec(&rq->prio.nr_blocked);
  128. if (!waitqueue_active(wq))
  129. __clear_bit(prio, rq->prio.bitmap);
  130. }
  131. static inline int is_best_prio(struct spu_runqueue *rq)
  132. {
  133. int best_prio;
  134. best_prio = sched_find_first_bit(rq->prio.bitmap);
  135. return (current->prio < best_prio) ? 1 : 0;
  136. }
  137. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  138. {
  139. /* Global TLBIE broadcast required with SPEs. */
  140. #if (NR_CPUS > 1)
  141. __cpus_setall(&mm->cpu_vm_mask, NR_CPUS);
  142. #else
  143. __cpus_setall(&mm->cpu_vm_mask, NR_CPUS+1); /* is this ok? */
  144. #endif
  145. }
  146. static inline void bind_context(struct spu *spu, struct spu_context *ctx)
  147. {
  148. pr_debug("%s: pid=%d SPU=%d\n", __FUNCTION__, current->pid,
  149. spu->number);
  150. spu->ctx = ctx;
  151. spu->flags = 0;
  152. ctx->spu = spu;
  153. ctx->ops = &spu_hw_ops;
  154. spu->pid = current->pid;
  155. spu->prio = current->prio;
  156. spu->mm = ctx->owner;
  157. mm_needs_global_tlbie(spu->mm);
  158. spu->ibox_callback = spufs_ibox_callback;
  159. spu->wbox_callback = spufs_wbox_callback;
  160. spu->stop_callback = spufs_stop_callback;
  161. mb();
  162. spu_unmap_mappings(ctx);
  163. spu_restore(&ctx->csa, spu);
  164. }
  165. static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
  166. {
  167. pr_debug("%s: unbind pid=%d SPU=%d\n", __FUNCTION__,
  168. spu->pid, spu->number);
  169. spu_unmap_mappings(ctx);
  170. spu_save(&ctx->csa, spu);
  171. ctx->state = SPU_STATE_SAVED;
  172. spu->ibox_callback = NULL;
  173. spu->wbox_callback = NULL;
  174. spu->stop_callback = NULL;
  175. spu->mm = NULL;
  176. spu->pid = 0;
  177. spu->prio = MAX_PRIO;
  178. ctx->ops = &spu_backing_ops;
  179. ctx->spu = NULL;
  180. spu->ctx = NULL;
  181. }
  182. static struct spu *preempt_active(struct spu_runqueue *rq)
  183. {
  184. struct list_head *p;
  185. struct spu *worst, *spu;
  186. worst = list_entry(rq->active_list.next, struct spu, sched_list);
  187. list_for_each(p, &rq->active_list) {
  188. spu = list_entry(p, struct spu, sched_list);
  189. if (spu->prio > worst->prio) {
  190. worst = spu;
  191. }
  192. }
  193. if (current->prio < worst->prio) {
  194. struct spu_context *ctx = worst->ctx;
  195. spu = worst;
  196. if (down_write_trylock(&ctx->state_sema)) {
  197. pr_debug("%s: booting pid=%d from SPU %d\n",
  198. __FUNCTION__, spu->pid, spu->number);
  199. del_active(rq, spu);
  200. up(&rq->sem);
  201. wake_up_all(&ctx->stop_wq);
  202. ctx->ops->runcntl_stop(ctx);
  203. unbind_context(spu, ctx);
  204. up_write(&ctx->state_sema);
  205. return spu;
  206. }
  207. }
  208. return NULL;
  209. }
  210. static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags)
  211. {
  212. struct spu_runqueue *rq;
  213. struct spu *spu = NULL;
  214. rq = spu_rq();
  215. down(&rq->sem);
  216. for (;;) {
  217. if (rq->nr_idle > 0) {
  218. if (is_best_prio(rq)) {
  219. /* Fall through. */
  220. spu = del_idle(rq);
  221. break;
  222. } else {
  223. prio_wakeup(rq);
  224. up(&rq->sem);
  225. yield();
  226. if (signal_pending(current)) {
  227. return NULL;
  228. }
  229. rq = spu_rq();
  230. down(&rq->sem);
  231. continue;
  232. }
  233. } else {
  234. if (is_best_prio(rq)) {
  235. if ((spu = preempt_active(rq)) != NULL)
  236. return spu;
  237. }
  238. prio_wait(rq, ctx, flags);
  239. if (signal_pending(current)) {
  240. prio_wakeup(rq);
  241. spu = NULL;
  242. break;
  243. }
  244. continue;
  245. }
  246. }
  247. up(&rq->sem);
  248. return spu;
  249. }
  250. static void put_idle_spu(struct spu *spu)
  251. {
  252. struct spu_runqueue *rq = spu->rq;
  253. down(&rq->sem);
  254. add_idle(rq, spu);
  255. prio_wakeup(rq);
  256. up(&rq->sem);
  257. }
  258. static int get_active_spu(struct spu *spu)
  259. {
  260. struct spu_runqueue *rq = spu->rq;
  261. struct list_head *p;
  262. struct spu *tmp;
  263. int rc = 0;
  264. down(&rq->sem);
  265. list_for_each(p, &rq->active_list) {
  266. tmp = list_entry(p, struct spu, sched_list);
  267. if (tmp == spu) {
  268. del_active(rq, spu);
  269. rc = 1;
  270. break;
  271. }
  272. }
  273. up(&rq->sem);
  274. return rc;
  275. }
  276. static void put_active_spu(struct spu *spu)
  277. {
  278. struct spu_runqueue *rq = spu->rq;
  279. down(&rq->sem);
  280. add_active(rq, spu);
  281. up(&rq->sem);
  282. }
  283. /* Lock order:
  284. * spu_activate() & spu_deactivate() require the
  285. * caller to have down_write(&ctx->state_sema).
  286. *
  287. * The rq->sem is breifly held (inside or outside a
  288. * given ctx lock) for list management, but is never
  289. * held during save/restore.
  290. */
  291. int spu_activate(struct spu_context *ctx, u64 flags)
  292. {
  293. struct spu *spu;
  294. if (ctx->spu)
  295. return 0;
  296. spu = get_idle_spu(ctx, flags);
  297. if (!spu)
  298. return (signal_pending(current)) ? -ERESTARTSYS : -EAGAIN;
  299. bind_context(spu, ctx);
  300. put_active_spu(spu);
  301. return 0;
  302. }
  303. void spu_deactivate(struct spu_context *ctx)
  304. {
  305. struct spu *spu;
  306. int needs_idle;
  307. spu = ctx->spu;
  308. if (!spu)
  309. return;
  310. needs_idle = get_active_spu(spu);
  311. unbind_context(spu, ctx);
  312. if (needs_idle)
  313. put_idle_spu(spu);
  314. }
  315. void spu_yield(struct spu_context *ctx)
  316. {
  317. struct spu *spu;
  318. int need_yield = 0;
  319. down_write(&ctx->state_sema);
  320. spu = ctx->spu;
  321. if (spu && (sched_find_first_bit(spu->rq->prio.bitmap) < MAX_PRIO)) {
  322. pr_debug("%s: yielding SPU %d\n", __FUNCTION__, spu->number);
  323. spu_deactivate(ctx);
  324. ctx->state = SPU_STATE_SAVED;
  325. need_yield = 1;
  326. }
  327. up_write(&ctx->state_sema);
  328. if (unlikely(need_yield))
  329. yield();
  330. }
  331. int __init spu_sched_init(void)
  332. {
  333. struct spu_runqueue *rq;
  334. struct spu *spu;
  335. int i;
  336. rq = spu_runqueues = kmalloc(sizeof(struct spu_runqueue), GFP_KERNEL);
  337. if (!rq) {
  338. printk(KERN_WARNING "%s: Unable to allocate runqueues.\n",
  339. __FUNCTION__);
  340. return 1;
  341. }
  342. memset(rq, 0, sizeof(struct spu_runqueue));
  343. init_MUTEX(&rq->sem);
  344. INIT_LIST_HEAD(&rq->active_list);
  345. INIT_LIST_HEAD(&rq->idle_list);
  346. rq->nr_active = 0;
  347. rq->nr_idle = 0;
  348. rq->nr_switches = 0;
  349. atomic_set(&rq->prio.nr_blocked, 0);
  350. for (i = 0; i < MAX_PRIO; i++) {
  351. init_waitqueue_head(&rq->prio.waitq[i]);
  352. __clear_bit(i, rq->prio.bitmap);
  353. }
  354. __set_bit(MAX_PRIO, rq->prio.bitmap);
  355. for (;;) {
  356. spu = spu_alloc();
  357. if (!spu)
  358. break;
  359. pr_debug("%s: adding SPU[%d]\n", __FUNCTION__, spu->number);
  360. add_idle(rq, spu);
  361. spu->rq = rq;
  362. }
  363. if (!rq->nr_idle) {
  364. printk(KERN_WARNING "%s: No available SPUs.\n", __FUNCTION__);
  365. kfree(rq);
  366. return 1;
  367. }
  368. return 0;
  369. }
  370. void __exit spu_sched_exit(void)
  371. {
  372. struct spu_runqueue *rq = spu_rq();
  373. struct spu *spu;
  374. if (!rq) {
  375. printk(KERN_WARNING "%s: no runqueues!\n", __FUNCTION__);
  376. return;
  377. }
  378. while (rq->nr_idle > 0) {
  379. spu = del_idle(rq);
  380. if (!spu)
  381. break;
  382. spu_free(spu);
  383. }
  384. kfree(rq);
  385. }