sched.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* sched.c - SPU scheduler.
  2. *
  3. * Copyright (C) IBM 2005
  4. * Author: Mark Nutter <mnutter@us.ibm.com>
  5. *
  6. * 2006-03-31 NUMA domains added.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #undef DEBUG
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/sched.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/completion.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/smp.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/stddef.h>
  33. #include <linux/unistd.h>
  34. #include <linux/numa.h>
  35. #include <linux/mutex.h>
  36. #include <linux/notifier.h>
  37. #include <asm/io.h>
  38. #include <asm/mmu_context.h>
  39. #include <asm/spu.h>
  40. #include <asm/spu_csa.h>
  41. #include <asm/spu_priv1.h>
  42. #include "spufs.h"
  43. #define SPU_TIMESLICE (HZ)
  44. struct spu_prio_array {
  45. DECLARE_BITMAP(bitmap, MAX_PRIO);
  46. struct list_head runq[MAX_PRIO];
  47. spinlock_t runq_lock;
  48. struct list_head active_list[MAX_NUMNODES];
  49. struct mutex active_mutex[MAX_NUMNODES];
  50. };
  51. static struct spu_prio_array *spu_prio;
  52. static struct workqueue_struct *spu_sched_wq;
  53. static inline int node_allowed(int node)
  54. {
  55. cpumask_t mask;
  56. if (!nr_cpus_node(node))
  57. return 0;
  58. mask = node_to_cpumask(node);
  59. if (!cpus_intersects(mask, current->cpus_allowed))
  60. return 0;
  61. return 1;
  62. }
  63. void spu_start_tick(struct spu_context *ctx)
  64. {
  65. if (ctx->policy == SCHED_RR)
  66. queue_delayed_work(spu_sched_wq, &ctx->sched_work, SPU_TIMESLICE);
  67. }
  68. void spu_stop_tick(struct spu_context *ctx)
  69. {
  70. if (ctx->policy == SCHED_RR)
  71. cancel_delayed_work(&ctx->sched_work);
  72. }
  73. void spu_sched_tick(struct work_struct *work)
  74. {
  75. struct spu_context *ctx =
  76. container_of(work, struct spu_context, sched_work.work);
  77. struct spu *spu;
  78. int rearm = 1;
  79. mutex_lock(&ctx->state_mutex);
  80. spu = ctx->spu;
  81. if (spu) {
  82. int best = sched_find_first_bit(spu_prio->bitmap);
  83. if (best <= ctx->prio) {
  84. spu_deactivate(ctx);
  85. rearm = 0;
  86. }
  87. }
  88. mutex_unlock(&ctx->state_mutex);
  89. if (rearm)
  90. spu_start_tick(ctx);
  91. }
  92. /**
  93. * spu_add_to_active_list - add spu to active list
  94. * @spu: spu to add to the active list
  95. */
  96. static void spu_add_to_active_list(struct spu *spu)
  97. {
  98. mutex_lock(&spu_prio->active_mutex[spu->node]);
  99. list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
  100. mutex_unlock(&spu_prio->active_mutex[spu->node]);
  101. }
  102. /**
  103. * spu_remove_from_active_list - remove spu from active list
  104. * @spu: spu to remove from the active list
  105. */
  106. static void spu_remove_from_active_list(struct spu *spu)
  107. {
  108. int node = spu->node;
  109. mutex_lock(&spu_prio->active_mutex[node]);
  110. list_del_init(&spu->list);
  111. mutex_unlock(&spu_prio->active_mutex[node]);
  112. }
  113. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  114. {
  115. int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
  116. /* Global TLBIE broadcast required with SPEs. */
  117. __cpus_setall(&mm->cpu_vm_mask, nr);
  118. }
  119. static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
  120. static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
  121. {
  122. blocking_notifier_call_chain(&spu_switch_notifier,
  123. ctx ? ctx->object_id : 0, spu);
  124. }
  125. int spu_switch_event_register(struct notifier_block * n)
  126. {
  127. return blocking_notifier_chain_register(&spu_switch_notifier, n);
  128. }
  129. int spu_switch_event_unregister(struct notifier_block * n)
  130. {
  131. return blocking_notifier_chain_unregister(&spu_switch_notifier, n);
  132. }
  133. /**
  134. * spu_bind_context - bind spu context to physical spu
  135. * @spu: physical spu to bind to
  136. * @ctx: context to bind
  137. */
  138. static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
  139. {
  140. pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
  141. spu->number, spu->node);
  142. spu->ctx = ctx;
  143. spu->flags = 0;
  144. ctx->spu = spu;
  145. ctx->ops = &spu_hw_ops;
  146. spu->pid = current->pid;
  147. spu->mm = ctx->owner;
  148. mm_needs_global_tlbie(spu->mm);
  149. spu->ibox_callback = spufs_ibox_callback;
  150. spu->wbox_callback = spufs_wbox_callback;
  151. spu->stop_callback = spufs_stop_callback;
  152. spu->mfc_callback = spufs_mfc_callback;
  153. spu->dma_callback = spufs_dma_callback;
  154. mb();
  155. spu_unmap_mappings(ctx);
  156. spu_restore(&ctx->csa, spu);
  157. spu->timestamp = jiffies;
  158. spu_cpu_affinity_set(spu, raw_smp_processor_id());
  159. spu_switch_notify(spu, ctx);
  160. spu_add_to_active_list(spu);
  161. ctx->state = SPU_STATE_RUNNABLE;
  162. }
  163. /**
  164. * spu_unbind_context - unbind spu context from physical spu
  165. * @spu: physical spu to unbind from
  166. * @ctx: context to unbind
  167. */
  168. static void spu_unbind_context(struct spu *spu, struct spu_context *ctx)
  169. {
  170. pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
  171. spu->pid, spu->number, spu->node);
  172. spu_remove_from_active_list(spu);
  173. spu_switch_notify(spu, NULL);
  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->dma_callback = NULL;
  183. spu->mm = NULL;
  184. spu->pid = 0;
  185. ctx->ops = &spu_backing_ops;
  186. ctx->spu = NULL;
  187. spu->flags = 0;
  188. spu->ctx = NULL;
  189. }
  190. /**
  191. * spu_add_to_rq - add a context to the runqueue
  192. * @ctx: context to add
  193. */
  194. static void spu_add_to_rq(struct spu_context *ctx)
  195. {
  196. spin_lock(&spu_prio->runq_lock);
  197. list_add_tail(&ctx->rq, &spu_prio->runq[ctx->prio]);
  198. set_bit(ctx->prio, spu_prio->bitmap);
  199. spin_unlock(&spu_prio->runq_lock);
  200. }
  201. /**
  202. * spu_del_from_rq - remove a context from the runqueue
  203. * @ctx: context to remove
  204. */
  205. static void spu_del_from_rq(struct spu_context *ctx)
  206. {
  207. spin_lock(&spu_prio->runq_lock);
  208. list_del_init(&ctx->rq);
  209. if (list_empty(&spu_prio->runq[ctx->prio]))
  210. clear_bit(ctx->prio, spu_prio->bitmap);
  211. spin_unlock(&spu_prio->runq_lock);
  212. }
  213. /**
  214. * spu_grab_context - remove one context from the runqueue
  215. * @prio: priority of the context to be removed
  216. *
  217. * This function removes one context from the runqueue for priority @prio.
  218. * If there is more than one context with the given priority the first
  219. * task on the runqueue will be taken.
  220. *
  221. * Returns the spu_context it just removed.
  222. *
  223. * Must be called with spu_prio->runq_lock held.
  224. */
  225. static struct spu_context *spu_grab_context(int prio)
  226. {
  227. struct list_head *rq = &spu_prio->runq[prio];
  228. if (list_empty(rq))
  229. return NULL;
  230. return list_entry(rq->next, struct spu_context, rq);
  231. }
  232. static void spu_prio_wait(struct spu_context *ctx)
  233. {
  234. DEFINE_WAIT(wait);
  235. set_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
  236. prepare_to_wait_exclusive(&ctx->stop_wq, &wait, TASK_INTERRUPTIBLE);
  237. if (!signal_pending(current)) {
  238. mutex_unlock(&ctx->state_mutex);
  239. schedule();
  240. mutex_lock(&ctx->state_mutex);
  241. }
  242. __set_current_state(TASK_RUNNING);
  243. remove_wait_queue(&ctx->stop_wq, &wait);
  244. clear_bit(SPU_SCHED_WAKE, &ctx->sched_flags);
  245. }
  246. /**
  247. * spu_reschedule - try to find a runnable context for a spu
  248. * @spu: spu available
  249. *
  250. * This function is called whenever a spu becomes idle. It looks for the
  251. * most suitable runnable spu context and schedules it for execution.
  252. */
  253. static void spu_reschedule(struct spu *spu)
  254. {
  255. int best;
  256. spu_free(spu);
  257. spin_lock(&spu_prio->runq_lock);
  258. best = sched_find_first_bit(spu_prio->bitmap);
  259. if (best < MAX_PRIO) {
  260. struct spu_context *ctx = spu_grab_context(best);
  261. if (ctx && test_bit(SPU_SCHED_WAKE, &ctx->sched_flags))
  262. wake_up(&ctx->stop_wq);
  263. }
  264. spin_unlock(&spu_prio->runq_lock);
  265. }
  266. static struct spu *spu_get_idle(struct spu_context *ctx)
  267. {
  268. struct spu *spu = NULL;
  269. int node = cpu_to_node(raw_smp_processor_id());
  270. int n;
  271. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  272. node = (node < MAX_NUMNODES) ? node : 0;
  273. if (!node_allowed(node))
  274. continue;
  275. spu = spu_alloc_node(node);
  276. if (spu)
  277. break;
  278. }
  279. return spu;
  280. }
  281. /**
  282. * find_victim - find a lower priority context to preempt
  283. * @ctx: canidate context for running
  284. *
  285. * Returns the freed physical spu to run the new context on.
  286. */
  287. static struct spu *find_victim(struct spu_context *ctx)
  288. {
  289. struct spu_context *victim = NULL;
  290. struct spu *spu;
  291. int node, n;
  292. /*
  293. * Look for a possible preemption candidate on the local node first.
  294. * If there is no candidate look at the other nodes. This isn't
  295. * exactly fair, but so far the whole spu schedule tries to keep
  296. * a strong node affinity. We might want to fine-tune this in
  297. * the future.
  298. */
  299. restart:
  300. node = cpu_to_node(raw_smp_processor_id());
  301. for (n = 0; n < MAX_NUMNODES; n++, node++) {
  302. node = (node < MAX_NUMNODES) ? node : 0;
  303. if (!node_allowed(node))
  304. continue;
  305. mutex_lock(&spu_prio->active_mutex[node]);
  306. list_for_each_entry(spu, &spu_prio->active_list[node], list) {
  307. struct spu_context *tmp = spu->ctx;
  308. if (tmp->rt_priority < ctx->rt_priority &&
  309. (!victim || tmp->rt_priority < victim->rt_priority))
  310. victim = spu->ctx;
  311. }
  312. mutex_unlock(&spu_prio->active_mutex[node]);
  313. if (victim) {
  314. /*
  315. * This nests ctx->state_mutex, but we always lock
  316. * higher priority contexts before lower priority
  317. * ones, so this is safe until we introduce
  318. * priority inheritance schemes.
  319. */
  320. if (!mutex_trylock(&victim->state_mutex)) {
  321. victim = NULL;
  322. goto restart;
  323. }
  324. spu = victim->spu;
  325. if (!spu) {
  326. /*
  327. * This race can happen because we've dropped
  328. * the active list mutex. No a problem, just
  329. * restart the search.
  330. */
  331. mutex_unlock(&victim->state_mutex);
  332. victim = NULL;
  333. goto restart;
  334. }
  335. spu_unbind_context(spu, victim);
  336. mutex_unlock(&victim->state_mutex);
  337. return spu;
  338. }
  339. }
  340. return NULL;
  341. }
  342. /**
  343. * spu_activate - find a free spu for a context and execute it
  344. * @ctx: spu context to schedule
  345. * @flags: flags (currently ignored)
  346. *
  347. * Tries to find a free spu to run @ctx. If no free spu is availble
  348. * add the context to the runqueue so it gets woken up once an spu
  349. * is available.
  350. */
  351. int spu_activate(struct spu_context *ctx, unsigned long flags)
  352. {
  353. if (ctx->spu)
  354. return 0;
  355. do {
  356. struct spu *spu;
  357. spu = spu_get_idle(ctx);
  358. /*
  359. * If this is a realtime thread we try to get it running by
  360. * preempting a lower priority thread.
  361. */
  362. if (!spu && ctx->rt_priority)
  363. spu = find_victim(ctx);
  364. if (spu) {
  365. spu_bind_context(spu, ctx);
  366. return 0;
  367. }
  368. spu_add_to_rq(ctx);
  369. if (!(flags & SPU_ACTIVATE_NOWAKE))
  370. spu_prio_wait(ctx);
  371. spu_del_from_rq(ctx);
  372. } while (!signal_pending(current));
  373. return -ERESTARTSYS;
  374. }
  375. /**
  376. * spu_deactivate - unbind a context from it's physical spu
  377. * @ctx: spu context to unbind
  378. *
  379. * Unbind @ctx from the physical spu it is running on and schedule
  380. * the highest priority context to run on the freed physical spu.
  381. */
  382. void spu_deactivate(struct spu_context *ctx)
  383. {
  384. struct spu *spu = ctx->spu;
  385. if (spu) {
  386. spu_unbind_context(spu, ctx);
  387. spu_reschedule(spu);
  388. }
  389. }
  390. /**
  391. * spu_yield - yield a physical spu if others are waiting
  392. * @ctx: spu context to yield
  393. *
  394. * Check if there is a higher priority context waiting and if yes
  395. * unbind @ctx from the physical spu and schedule the highest
  396. * priority context to run on the freed physical spu instead.
  397. */
  398. void spu_yield(struct spu_context *ctx)
  399. {
  400. struct spu *spu;
  401. int need_yield = 0;
  402. if (mutex_trylock(&ctx->state_mutex)) {
  403. if ((spu = ctx->spu) != NULL) {
  404. int best = sched_find_first_bit(spu_prio->bitmap);
  405. if (best < MAX_PRIO) {
  406. pr_debug("%s: yielding SPU %d NODE %d\n",
  407. __FUNCTION__, spu->number, spu->node);
  408. spu_deactivate(ctx);
  409. need_yield = 1;
  410. }
  411. }
  412. mutex_unlock(&ctx->state_mutex);
  413. }
  414. if (unlikely(need_yield))
  415. yield();
  416. }
  417. int __init spu_sched_init(void)
  418. {
  419. int i;
  420. spu_sched_wq = create_singlethread_workqueue("spusched");
  421. if (!spu_sched_wq)
  422. return 1;
  423. spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
  424. if (!spu_prio) {
  425. printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
  426. __FUNCTION__);
  427. destroy_workqueue(spu_sched_wq);
  428. return 1;
  429. }
  430. for (i = 0; i < MAX_PRIO; i++) {
  431. INIT_LIST_HEAD(&spu_prio->runq[i]);
  432. __clear_bit(i, spu_prio->bitmap);
  433. }
  434. __set_bit(MAX_PRIO, spu_prio->bitmap);
  435. for (i = 0; i < MAX_NUMNODES; i++) {
  436. mutex_init(&spu_prio->active_mutex[i]);
  437. INIT_LIST_HEAD(&spu_prio->active_list[i]);
  438. }
  439. spin_lock_init(&spu_prio->runq_lock);
  440. return 0;
  441. }
  442. void __exit spu_sched_exit(void)
  443. {
  444. struct spu *spu, *tmp;
  445. int node;
  446. for (node = 0; node < MAX_NUMNODES; node++) {
  447. mutex_lock(&spu_prio->active_mutex[node]);
  448. list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
  449. list) {
  450. list_del_init(&spu->list);
  451. spu_free(spu);
  452. }
  453. mutex_unlock(&spu_prio->active_mutex[node]);
  454. }
  455. kfree(spu_prio);
  456. destroy_workqueue(spu_sched_wq);
  457. }