[POWERPC] spufs: sched.c cleanups
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / cell / spufs / sched.c
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
23 #undef DEBUG
24
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/sched.h>
28 #include <linux/kernel.h>
29 #include <linux/mm.h>
30 #include <linux/completion.h>
31 #include <linux/vmalloc.h>
32 #include <linux/smp.h>
33 #include <linux/smp_lock.h>
34 #include <linux/stddef.h>
35 #include <linux/unistd.h>
36 #include <linux/numa.h>
37 #include <linux/mutex.h>
38 #include <linux/notifier.h>
39
40 #include <asm/io.h>
41 #include <asm/mmu_context.h>
42 #include <asm/spu.h>
43 #include <asm/spu_csa.h>
44 #include <asm/spu_priv1.h>
45 #include "spufs.h"
46
47 #define SPU_MIN_TIMESLICE       (100 * HZ / 1000)
48
49 #define SPU_BITMAP_SIZE (((MAX_PRIO+BITS_PER_LONG)/BITS_PER_LONG)+1)
50 struct spu_prio_array {
51         unsigned long bitmap[SPU_BITMAP_SIZE];
52         wait_queue_head_t waitq[MAX_PRIO];
53         struct list_head active_list[MAX_NUMNODES];
54         struct mutex active_mutex[MAX_NUMNODES];
55 };
56
57 static struct spu_prio_array *spu_prio;
58
59 static inline int node_allowed(int node)
60 {
61         cpumask_t mask;
62
63         if (!nr_cpus_node(node))
64                 return 0;
65         mask = node_to_cpumask(node);
66         if (!cpus_intersects(mask, current->cpus_allowed))
67                 return 0;
68         return 1;
69 }
70
71 /**
72  * spu_add_to_active_list - add spu to active list
73  * @spu:        spu to add to the active list
74  */
75 static void spu_add_to_active_list(struct spu *spu)
76 {
77         mutex_lock(&spu_prio->active_mutex[spu->node]);
78         list_add_tail(&spu->list, &spu_prio->active_list[spu->node]);
79         mutex_unlock(&spu_prio->active_mutex[spu->node]);
80 }
81
82 /**
83  * spu_remove_from_active_list - remove spu from active list
84  * @spu:       spu to remove from the active list
85  *
86  * This function removes an spu from the active list.  If the spu was
87  * found on the active list the function returns 1, else it doesn't do
88  * anything and returns 0.
89  */
90 static int spu_remove_from_active_list(struct spu *spu)
91 {
92         int node = spu->node;
93         struct spu *tmp;
94         int rc = 0;
95
96         mutex_lock(&spu_prio->active_mutex[node]);
97         list_for_each_entry(tmp, &spu_prio->active_list[node], list) {
98                 if (tmp == spu) {
99                         list_del_init(&spu->list);
100                         rc = 1;
101                         break;
102                 }
103         }
104         mutex_unlock(&spu_prio->active_mutex[node]);
105         return rc;
106 }
107
108 static inline void mm_needs_global_tlbie(struct mm_struct *mm)
109 {
110         int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
111
112         /* Global TLBIE broadcast required with SPEs. */
113         __cpus_setall(&mm->cpu_vm_mask, nr);
114 }
115
116 static BLOCKING_NOTIFIER_HEAD(spu_switch_notifier);
117
118 static void spu_switch_notify(struct spu *spu, struct spu_context *ctx)
119 {
120         blocking_notifier_call_chain(&spu_switch_notifier,
121                             ctx ? ctx->object_id : 0, spu);
122 }
123
124 int spu_switch_event_register(struct notifier_block * n)
125 {
126         return blocking_notifier_chain_register(&spu_switch_notifier, n);
127 }
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 /**
135  * spu_bind_context - bind spu context to physical spu
136  * @spu:        physical spu to bind to
137  * @ctx:        context to bind
138  */
139 static void spu_bind_context(struct spu *spu, struct spu_context *ctx)
140 {
141         pr_debug("%s: pid=%d SPU=%d NODE=%d\n", __FUNCTION__, current->pid,
142                  spu->number, spu->node);
143         spu->ctx = ctx;
144         spu->flags = 0;
145         ctx->spu = spu;
146         ctx->ops = &spu_hw_ops;
147         spu->pid = current->pid;
148         spu->prio = current->prio;
149         spu->mm = ctx->owner;
150         mm_needs_global_tlbie(spu->mm);
151         spu->ibox_callback = spufs_ibox_callback;
152         spu->wbox_callback = spufs_wbox_callback;
153         spu->stop_callback = spufs_stop_callback;
154         spu->mfc_callback = spufs_mfc_callback;
155         spu->dma_callback = spufs_dma_callback;
156         mb();
157         spu_unmap_mappings(ctx);
158         spu_restore(&ctx->csa, spu);
159         spu->timestamp = jiffies;
160         spu_cpu_affinity_set(spu, raw_smp_processor_id());
161         spu_switch_notify(spu, ctx);
162         spu_add_to_active_list(spu);
163         ctx->state = SPU_STATE_RUNNABLE;
164 }
165
166 /**
167  * spu_unbind_context - unbind spu context from physical spu
168  * @spu:        physical spu to unbind from
169  * @ctx:        context to unbind
170  *
171  * If the spu was on the active list the function returns 1, else 0.
172  */
173 static int spu_unbind_context(struct spu *spu, struct spu_context *ctx)
174 {
175         int was_active = spu_remove_from_active_list(spu);
176
177         pr_debug("%s: unbind pid=%d SPU=%d NODE=%d\n", __FUNCTION__,
178                  spu->pid, spu->number, spu->node);
179
180         spu_switch_notify(spu, NULL);
181         spu_unmap_mappings(ctx);
182         spu_save(&ctx->csa, spu);
183         spu->timestamp = jiffies;
184         ctx->state = SPU_STATE_SAVED;
185         spu->ibox_callback = NULL;
186         spu->wbox_callback = NULL;
187         spu->stop_callback = NULL;
188         spu->mfc_callback = NULL;
189         spu->dma_callback = NULL;
190         spu->mm = NULL;
191         spu->pid = 0;
192         spu->prio = MAX_PRIO;
193         ctx->ops = &spu_backing_ops;
194         ctx->spu = NULL;
195         spu->flags = 0;
196         spu->ctx = NULL;
197
198         return was_active;
199 }
200
201 static inline void spu_add_wq(wait_queue_head_t * wq, wait_queue_t * wait,
202                               int prio)
203 {
204         prepare_to_wait_exclusive(wq, wait, TASK_INTERRUPTIBLE);
205         set_bit(prio, spu_prio->bitmap);
206 }
207
208 static inline void spu_del_wq(wait_queue_head_t * wq, wait_queue_t * wait,
209                               int prio)
210 {
211         u64 flags;
212
213         __set_current_state(TASK_RUNNING);
214
215         spin_lock_irqsave(&wq->lock, flags);
216
217         remove_wait_queue_locked(wq, wait);
218         if (list_empty(&wq->task_list))
219                 clear_bit(prio, spu_prio->bitmap);
220
221         spin_unlock_irqrestore(&wq->lock, flags);
222 }
223
224 static void spu_prio_wait(struct spu_context *ctx, u64 flags)
225 {
226         int prio = current->prio;
227         wait_queue_head_t *wq = &spu_prio->waitq[prio];
228         DEFINE_WAIT(wait);
229
230         if (ctx->spu)
231                 return;
232
233         spu_add_wq(wq, &wait, prio);
234
235         if (!signal_pending(current)) {
236                 up_write(&ctx->state_sema);
237                 pr_debug("%s: pid=%d prio=%d\n", __FUNCTION__,
238                          current->pid, current->prio);
239                 schedule();
240                 down_write(&ctx->state_sema);
241         }
242
243         spu_del_wq(wq, &wait, prio);
244 }
245
246 static void spu_prio_wakeup(void)
247 {
248         int best = sched_find_first_bit(spu_prio->bitmap);
249         if (best < MAX_PRIO) {
250                 wait_queue_head_t *wq = &spu_prio->waitq[best];
251                 wake_up_interruptible_nr(wq, 1);
252         }
253 }
254
255 static struct spu *spu_get_idle(struct spu_context *ctx, u64 flags)
256 {
257         struct spu *spu = NULL;
258         int node = cpu_to_node(raw_smp_processor_id());
259         int n;
260
261         for (n = 0; n < MAX_NUMNODES; n++, node++) {
262                 node = (node < MAX_NUMNODES) ? node : 0;
263                 if (!node_allowed(node))
264                         continue;
265                 spu = spu_alloc_node(node);
266                 if (spu)
267                         break;
268         }
269         return spu;
270 }
271
272 static inline struct spu *spu_get(struct spu_context *ctx, u64 flags)
273 {
274         /* Future: spu_get_idle() if possible,
275          * otherwise try to preempt an active
276          * context.
277          */
278         return spu_get_idle(ctx, flags);
279 }
280
281 /* The three externally callable interfaces
282  * for the scheduler begin here.
283  *
284  *      spu_activate    - bind a context to SPU, waiting as needed.
285  *      spu_deactivate  - unbind a context from its SPU.
286  *      spu_yield       - yield an SPU if others are waiting.
287  */
288
289 int spu_activate(struct spu_context *ctx, u64 flags)
290 {
291         struct spu *spu;
292         int ret = 0;
293
294         for (;;) {
295                 if (ctx->spu)
296                         return 0;
297                 spu = spu_get(ctx, flags);
298                 if (spu != NULL) {
299                         if (ctx->spu != NULL) {
300                                 spu_free(spu);
301                                 spu_prio_wakeup();
302                                 break;
303                         }
304                         spu_bind_context(spu, ctx);
305                         break;
306                 }
307                 spu_prio_wait(ctx, flags);
308                 if (signal_pending(current)) {
309                         ret = -ERESTARTSYS;
310                         spu_prio_wakeup();
311                         break;
312                 }
313         }
314         return ret;
315 }
316
317 void spu_deactivate(struct spu_context *ctx)
318 {
319         struct spu *spu;
320         int was_active;
321
322         spu = ctx->spu;
323         if (!spu)
324                 return;
325         was_active = spu_unbind_context(spu, ctx);
326         if (was_active) {
327                 spu_free(spu);
328                 spu_prio_wakeup();
329         }
330 }
331
332 void spu_yield(struct spu_context *ctx)
333 {
334         struct spu *spu;
335         int need_yield = 0;
336
337         if (down_write_trylock(&ctx->state_sema)) {
338                 if ((spu = ctx->spu) != NULL) {
339                         int best = sched_find_first_bit(spu_prio->bitmap);
340                         if (best < MAX_PRIO) {
341                                 pr_debug("%s: yielding SPU %d NODE %d\n",
342                                          __FUNCTION__, spu->number, spu->node);
343                                 spu_deactivate(ctx);
344                                 need_yield = 1;
345                         } else {
346                                 spu->prio = MAX_PRIO;
347                         }
348                 }
349                 up_write(&ctx->state_sema);
350         }
351         if (unlikely(need_yield))
352                 yield();
353 }
354
355 int __init spu_sched_init(void)
356 {
357         int i;
358
359         spu_prio = kzalloc(sizeof(struct spu_prio_array), GFP_KERNEL);
360         if (!spu_prio) {
361                 printk(KERN_WARNING "%s: Unable to allocate priority queue.\n",
362                        __FUNCTION__);
363                 return 1;
364         }
365         for (i = 0; i < MAX_PRIO; i++) {
366                 init_waitqueue_head(&spu_prio->waitq[i]);
367                 __clear_bit(i, spu_prio->bitmap);
368         }
369         __set_bit(MAX_PRIO, spu_prio->bitmap);
370         for (i = 0; i < MAX_NUMNODES; i++) {
371                 mutex_init(&spu_prio->active_mutex[i]);
372                 INIT_LIST_HEAD(&spu_prio->active_list[i]);
373         }
374         return 0;
375 }
376
377 void __exit spu_sched_exit(void)
378 {
379         struct spu *spu, *tmp;
380         int node;
381
382         for (node = 0; node < MAX_NUMNODES; node++) {
383                 mutex_lock(&spu_prio->active_mutex[node]);
384                 list_for_each_entry_safe(spu, tmp, &spu_prio->active_list[node],
385                                          list) {
386                         list_del_init(&spu->list);
387                         spu_free(spu);
388                 }
389                 mutex_unlock(&spu_prio->active_mutex[node]);
390         }
391         kfree(spu_prio);
392 }