sched.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 <asm/spu_priv1.h>
  44. #include "spufs.h"
  45. #define SPU_MIN_TIMESLICE (100 * HZ / 1000)
  46. #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
  47. struct spu_prio_array {
  48. atomic_t nr_blocked;
  49. unsigned long bitmap[SPU_BITMAP_SIZE];
  50. wait_queue_head_t waitq[MAX_PRIO];
  51. };
  52. /* spu_runqueue - This is the main runqueue data structure for SPUs. */
  53. struct spu_runqueue {
  54. struct semaphore sem;
  55. unsigned long nr_active;
  56. unsigned long nr_idle;
  57. unsigned long nr_switches;
  58. struct list_head active_list;
  59. struct list_head idle_list;
  60. struct spu_prio_array prio;
  61. };
  62. static struct spu_runqueue *spu_runqueues = NULL;
  63. static inline struct spu_runqueue *spu_rq(void)
  64. {
  65. /* Future: make this a per-NODE array,
  66. * and use cpu_to_node(smp_processor_id())
  67. */
  68. return spu_runqueues;
  69. }
  70. static inline struct spu *del_idle(struct spu_runqueue *rq)
  71. {
  72. struct spu *spu;
  73. BUG_ON(rq->nr_idle <= 0);
  74. BUG_ON(list_empty(&rq->idle_list));
  75. /* Future: Move SPU out of low-power SRI state. */
  76. spu = list_entry(rq->idle_list.next, struct spu, sched_list);
  77. list_del_init(&spu->sched_list);
  78. rq->nr_idle--;
  79. return spu;
  80. }
  81. static inline void del_active(struct spu_runqueue *rq, struct spu *spu)
  82. {
  83. BUG_ON(rq->nr_active <= 0);
  84. BUG_ON(list_empty(&rq->active_list));
  85. list_del_init(&spu->sched_list);
  86. rq->nr_active--;
  87. }
  88. static inline void add_idle(struct spu_runqueue *rq, struct spu *spu)
  89. {
  90. /* Future: Put SPU into low-power SRI state. */
  91. list_add_tail(&spu->sched_list, &rq->idle_list);
  92. rq->nr_idle++;
  93. }
  94. static inline void add_active(struct spu_runqueue *rq, struct spu *spu)
  95. {
  96. rq->nr_active++;
  97. rq->nr_switches++;
  98. list_add_tail(&spu->sched_list, &rq->active_list);
  99. }
  100. static void prio_wakeup(struct spu_runqueue *rq)
  101. {
  102. if (atomic_read(&rq->prio.nr_blocked) && rq->nr_idle) {
  103. int best = sched_find_first_bit(rq->prio.bitmap);
  104. if (best < MAX_PRIO) {
  105. wait_queue_head_t *wq = &rq->prio.waitq[best];
  106. wake_up_interruptible_nr(wq, 1);
  107. }
  108. }
  109. }
  110. static void prio_wait(struct spu_runqueue *rq, struct spu_context *ctx,
  111. u64 flags)
  112. {
  113. int prio = current->prio;
  114. wait_queue_head_t *wq = &rq->prio.waitq[prio];
  115. DEFINE_WAIT(wait);
  116. __set_bit(prio, rq->prio.bitmap);
  117. atomic_inc(&rq->prio.nr_blocked);
  118. prepare_to_wait_exclusive(wq, &wait, TASK_INTERRUPTIBLE);
  119. if (!signal_pending(current)) {
  120. up(&rq->sem);
  121. up_write(&ctx->state_sema);
  122. pr_debug("%s: pid=%d prio=%d\n", __FUNCTION__,
  123. current->pid, current->prio);
  124. schedule();
  125. down_write(&ctx->state_sema);
  126. down(&rq->sem);
  127. }
  128. finish_wait(wq, &wait);
  129. atomic_dec(&rq->prio.nr_blocked);
  130. if (!waitqueue_active(wq))
  131. __clear_bit(prio, rq->prio.bitmap);
  132. }
  133. static inline int is_best_prio(struct spu_runqueue *rq)
  134. {
  135. int best_prio;
  136. best_prio = sched_find_first_bit(rq->prio.bitmap);
  137. return (current->prio < best_prio) ? 1 : 0;
  138. }
  139. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  140. {
  141. /* Global TLBIE broadcast required with SPEs. */
  142. #if (NR_CPUS > 1)
  143. __cpus_setall(&mm->cpu_vm_mask, NR_CPUS);
  144. #else
  145. __cpus_setall(&mm->cpu_vm_mask, NR_CPUS+1); /* is this ok? */
  146. #endif
  147. }
  148. static inline void bind_context(struct spu *spu, struct spu_context *ctx)
  149. {
  150. pr_debug("%s: pid=%d SPU=%d\n", __FUNCTION__, current->pid,
  151. spu->number);
  152. spu->ctx = ctx;
  153. spu->flags = 0;
  154. ctx->flags = 0;
  155. ctx->spu = spu;
  156. ctx->ops = &spu_hw_ops;
  157. spu->pid = current->pid;
  158. spu->prio = current->prio;
  159. spu->mm = ctx->owner;
  160. mm_needs_global_tlbie(spu->mm);
  161. spu->ibox_callback = spufs_ibox_callback;
  162. spu->wbox_callback = spufs_wbox_callback;
  163. spu->stop_callback = spufs_stop_callback;
  164. spu->mfc_callback = spufs_mfc_callback;
  165. mb();
  166. spu_unmap_mappings(ctx);
  167. spu_restore(&ctx->csa, spu);
  168. spu->timestamp = jiffies;
  169. }
  170. static inline void unbind_context(struct spu *spu, struct spu_context *ctx)
  171. {
  172. pr_debug("%s: unbind pid=%d SPU=%d\n", __FUNCTION__,
  173. spu->pid, spu->number);
  174. spu_unmap_mappings(ctx);
  175. spu_save(&ctx->csa, spu);
  176. spu->timestamp = jiffies;
  177. ctx->state = SPU_STATE_SAVED;
  178. spu->ibox_callback = NULL;
  179. spu->wbox_callback = NULL;
  180. spu->stop_callback = NULL;
  181. spu->mfc_callback = NULL;
  182. spu->mm = NULL;
  183. spu->pid = 0;
  184. spu->prio = MAX_PRIO;
  185. ctx->ops = &spu_backing_ops;
  186. ctx->spu = NULL;
  187. ctx->flags = 0;
  188. spu->flags = 0;
  189. spu->ctx = NULL;
  190. }
  191. static void spu_reaper(void *data)
  192. {
  193. struct spu_context *ctx = data;
  194. struct spu *spu;
  195. down_write(&ctx->state_sema);
  196. spu = ctx->spu;
  197. if (spu && test_bit(SPU_CONTEXT_PREEMPT, &ctx->flags)) {
  198. if (atomic_read(&spu->rq->prio.nr_blocked)) {
  199. pr_debug("%s: spu=%d\n", __func__, spu->number);
  200. ctx->ops->runcntl_stop(ctx);
  201. spu_deactivate(ctx);
  202. wake_up_all(&ctx->stop_wq);
  203. } else {
  204. clear_bit(SPU_CONTEXT_PREEMPT, &ctx->flags);
  205. }
  206. }
  207. up_write(&ctx->state_sema);
  208. put_spu_context(ctx);
  209. }
  210. static void schedule_spu_reaper(struct spu_runqueue *rq, struct spu *spu)
  211. {
  212. struct spu_context *ctx = get_spu_context(spu->ctx);
  213. unsigned long now = jiffies;
  214. unsigned long expire = spu->timestamp + SPU_MIN_TIMESLICE;
  215. set_bit(SPU_CONTEXT_PREEMPT, &ctx->flags);
  216. INIT_WORK(&ctx->reap_work, spu_reaper, ctx);
  217. if (time_after(now, expire))
  218. schedule_work(&ctx->reap_work);
  219. else
  220. schedule_delayed_work(&ctx->reap_work, expire - now);
  221. }
  222. static void check_preempt_active(struct spu_runqueue *rq)
  223. {
  224. struct list_head *p;
  225. struct spu *worst = NULL;
  226. list_for_each(p, &rq->active_list) {
  227. struct spu *spu = list_entry(p, struct spu, sched_list);
  228. struct spu_context *ctx = spu->ctx;
  229. if (!test_bit(SPU_CONTEXT_PREEMPT, &ctx->flags)) {
  230. if (!worst || (spu->prio > worst->prio)) {
  231. worst = spu;
  232. }
  233. }
  234. }
  235. if (worst && (current->prio < worst->prio))
  236. schedule_spu_reaper(rq, worst);
  237. }
  238. static struct spu *get_idle_spu(struct spu_context *ctx, u64 flags)
  239. {
  240. struct spu_runqueue *rq;
  241. struct spu *spu = NULL;
  242. rq = spu_rq();
  243. down(&rq->sem);
  244. for (;;) {
  245. if (rq->nr_idle > 0) {
  246. if (is_best_prio(rq)) {
  247. /* Fall through. */
  248. spu = del_idle(rq);
  249. break;
  250. } else {
  251. prio_wakeup(rq);
  252. up(&rq->sem);
  253. yield();
  254. if (signal_pending(current)) {
  255. return NULL;
  256. }
  257. rq = spu_rq();
  258. down(&rq->sem);
  259. continue;
  260. }
  261. } else {
  262. check_preempt_active(rq);
  263. prio_wait(rq, ctx, flags);
  264. if (signal_pending(current)) {
  265. prio_wakeup(rq);
  266. spu = NULL;
  267. break;
  268. }
  269. continue;
  270. }
  271. }
  272. up(&rq->sem);
  273. return spu;
  274. }
  275. static void put_idle_spu(struct spu *spu)
  276. {
  277. struct spu_runqueue *rq = spu->rq;
  278. down(&rq->sem);
  279. add_idle(rq, spu);
  280. prio_wakeup(rq);
  281. up(&rq->sem);
  282. }
  283. static int get_active_spu(struct spu *spu)
  284. {
  285. struct spu_runqueue *rq = spu->rq;
  286. struct list_head *p;
  287. struct spu *tmp;
  288. int rc = 0;
  289. down(&rq->sem);
  290. list_for_each(p, &rq->active_list) {
  291. tmp = list_entry(p, struct spu, sched_list);
  292. if (tmp == spu) {
  293. del_active(rq, spu);
  294. rc = 1;
  295. break;
  296. }
  297. }
  298. up(&rq->sem);
  299. return rc;
  300. }
  301. static void put_active_spu(struct spu *spu)
  302. {
  303. struct spu_runqueue *rq = spu->rq;
  304. down(&rq->sem);
  305. add_active(rq, spu);
  306. up(&rq->sem);
  307. }
  308. /* Lock order:
  309. * spu_activate() & spu_deactivate() require the
  310. * caller to have down_write(&ctx->state_sema).
  311. *
  312. * The rq->sem is breifly held (inside or outside a
  313. * given ctx lock) for list management, but is never
  314. * held during save/restore.
  315. */
  316. int spu_activate(struct spu_context *ctx, u64 flags)
  317. {
  318. struct spu *spu;
  319. if (ctx->spu)
  320. return 0;
  321. spu = get_idle_spu(ctx, flags);
  322. if (!spu)
  323. return (signal_pending(current)) ? -ERESTARTSYS : -EAGAIN;
  324. bind_context(spu, ctx);
  325. /*
  326. * We're likely to wait for interrupts on the same
  327. * CPU that we are now on, so send them here.
  328. */
  329. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  330. put_active_spu(spu);
  331. return 0;
  332. }
  333. void spu_deactivate(struct spu_context *ctx)
  334. {
  335. struct spu *spu;
  336. int needs_idle;
  337. spu = ctx->spu;
  338. if (!spu)
  339. return;
  340. needs_idle = get_active_spu(spu);
  341. unbind_context(spu, ctx);
  342. if (needs_idle)
  343. put_idle_spu(spu);
  344. }
  345. void spu_yield(struct spu_context *ctx)
  346. {
  347. struct spu *spu;
  348. int need_yield = 0;
  349. down_write(&ctx->state_sema);
  350. spu = ctx->spu;
  351. if (spu && (sched_find_first_bit(spu->rq->prio.bitmap) < MAX_PRIO)) {
  352. pr_debug("%s: yielding SPU %d\n", __FUNCTION__, spu->number);
  353. spu_deactivate(ctx);
  354. ctx->state = SPU_STATE_SAVED;
  355. need_yield = 1;
  356. } else if (spu) {
  357. spu->prio = MAX_PRIO;
  358. }
  359. up_write(&ctx->state_sema);
  360. if (unlikely(need_yield))
  361. yield();
  362. }
  363. int __init spu_sched_init(void)
  364. {
  365. struct spu_runqueue *rq;
  366. struct spu *spu;
  367. int i;
  368. rq = spu_runqueues = kmalloc(sizeof(struct spu_runqueue), GFP_KERNEL);
  369. if (!rq) {
  370. printk(KERN_WARNING "%s: Unable to allocate runqueues.\n",
  371. __FUNCTION__);
  372. return 1;
  373. }
  374. memset(rq, 0, sizeof(struct spu_runqueue));
  375. init_MUTEX(&rq->sem);
  376. INIT_LIST_HEAD(&rq->active_list);
  377. INIT_LIST_HEAD(&rq->idle_list);
  378. rq->nr_active = 0;
  379. rq->nr_idle = 0;
  380. rq->nr_switches = 0;
  381. atomic_set(&rq->prio.nr_blocked, 0);
  382. for (i = 0; i < MAX_PRIO; i++) {
  383. init_waitqueue_head(&rq->prio.waitq[i]);
  384. __clear_bit(i, rq->prio.bitmap);
  385. }
  386. __set_bit(MAX_PRIO, rq->prio.bitmap);
  387. for (;;) {
  388. spu = spu_alloc();
  389. if (!spu)
  390. break;
  391. pr_debug("%s: adding SPU[%d]\n", __FUNCTION__, spu->number);
  392. add_idle(rq, spu);
  393. spu->rq = rq;
  394. spu->timestamp = jiffies;
  395. }
  396. if (!rq->nr_idle) {
  397. printk(KERN_WARNING "%s: No available SPUs.\n", __FUNCTION__);
  398. kfree(rq);
  399. return 1;
  400. }
  401. return 0;
  402. }
  403. void __exit spu_sched_exit(void)
  404. {
  405. struct spu_runqueue *rq = spu_rq();
  406. struct spu *spu;
  407. if (!rq) {
  408. printk(KERN_WARNING "%s: no runqueues!\n", __FUNCTION__);
  409. return;
  410. }
  411. while (rq->nr_idle > 0) {
  412. spu = del_idle(rq);
  413. if (!spu)
  414. break;
  415. spu_free(spu);
  416. }
  417. kfree(rq);
  418. }