sched.c 11 KB

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