2 * $Id: idle.c,v 1.68 1999/10/15 18:16:03 cort Exp $
4 * Idle daemon for PowerPC. Idle daemon will handle any action
5 * that needs to be taken when the system becomes idle.
7 * Written by Cort Dougan (cort@cs.nmt.edu)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <linux/errno.h>
16 #include <linux/sched.h>
17 #include <linux/kernel.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/stddef.h>
22 #include <linux/unistd.h>
23 #include <linux/ptrace.h>
24 #include <linux/malloc.h>
26 #include <asm/pgtable.h>
27 #include <asm/uaccess.h>
28 #include <asm/system.h>
30 #include <asm/processor.h>
32 #include <asm/cache.h>
34 void zero_paged(void);
35 void power_save(void);
36 void inline htab_reclaim(void);
38 unsigned long htab_reclaim_on
= 0;
39 unsigned long zero_paged_on
= 0;
40 unsigned long powersave_nap
= 0;
42 unsigned long *zero_cache
; /* head linked list of pre-zero'd pages */
43 atomic_t zerototal
; /* # pages zero'd over time */
44 atomic_t zeropage_hits
; /* # zero'd pages request that we've done */
45 atomic_t zero_sz
; /* # currently pre-zero'd pages */
46 atomic_t zeropage_calls
; /* # zero'd pages request that've been made */
50 /* endless loop with no priority at all */
52 current
->counter
= -100;
60 /*if ( !current->need_resched && zero_paged_on ) zero_paged();*/
61 if ( !current
->need_resched
&& htab_reclaim_on
) htab_reclaim();
62 if ( !current
->need_resched
) power_save();
65 if (current
->need_resched
)
73 * SMP entry into the idle task - calls the same thing as the
74 * non-smp versions. -- Cort
83 * Mark 'zombie' pte's in the hash table as invalid.
84 * This improves performance for the hash table reload code
85 * a bit since we don't consider unused pages as valid.
89 void inline htab_reclaim(void)
96 struct task_struct
*p
;
97 unsigned long valid
= 0;
98 extern PTE
*Hash
, *Hash_end
;
99 extern unsigned long Hash_size
;
101 /* if we don't have a htab */
102 if ( Hash_size
== 0 )
105 /* find a random place in the htab to start each time */
106 start
= &Hash
[jiffies
%(Hash_size
/sizeof(PTE
))];
107 /* go a different direction each time */
110 !current
->need_resched
&& (ptr
!= Hash_end
) && (ptr
!= Hash
);
114 if ( !reclaim_ptr
) reclaim_ptr
= Hash
;
115 while ( !current
->need_resched
)
118 if ( reclaim_ptr
== Hash_end
) reclaim_ptr
= Hash
;
125 if ( current
->need_resched
)
127 /* if this vsid/context is in use */
128 if ( (reclaim_ptr
->vsid
>> 4) == p
->mm
->context
)
136 /* this pte isn't used */
140 if ( current
->need_resched
) printk("need_resched: %lx\n", current
->need_resched
);
141 #endif /* CONFIG_8xx */
146 * Returns a pre-zero'd page from the list otherwise returns
149 unsigned long get_zero_page_fast(void)
151 unsigned long page
= 0;
153 atomic_inc(&zero_cache_calls
);
154 if ( zero_quicklist
)
156 /* atomically remove this page from the list */
157 register unsigned long tmp
;
158 asm ( "101:lwarx %1,0,%3\n" /* reserve zero_cache */
159 " lwz %0,0(%1)\n" /* get next -- new zero_cache */
160 " stwcx. %0,0,%3\n" /* update zero_cache */
161 " bne- 101b\n" /* if lost reservation try again */
162 : "=&r" (tmp
), "=&r" (page
), "+m" (zero_cache
)
163 : "r" (&zero_quicklist
)
166 /* if another cpu beat us above this can happen -- Cort */
169 #endif /* CONFIG_SMP */
170 /* we can update zerocount after the fact since it is not
171 * used for anything but control of a loop which doesn't
172 * matter since it won't affect anything if it zeros one
175 atomic_inc((atomic_t
*)&zero_cache_hits
);
176 atomic_dec((atomic_t
*)&zero_cache_sz
);
178 /* zero out the pointer to next in the page */
179 *(unsigned long *)page
= 0;
186 * Experimental stuff to zero out pages in the idle task
187 * to speed up get_free_pages(). Zero's out pages until
188 * we've reached the limit of zero'd pages. We handle
189 * reschedule()'s in here so when we return we know we've
190 * zero'd all we need to for now.
192 int zero_cache_water
[2] = { 25, 96 }; /* high and low water marks for zero cache */
193 void zero_paged(void)
195 unsigned long pageptr
= 0; /* current page being zero'd */
196 unsigned long bytecount
= 0;
197 register unsigned long tmp
;
200 if ( atomic_read(&zero_cache_sz
) >= zero_cache_water
[0] )
202 while ( (atomic_read(&zero_cache_sz
) < zero_cache_water
[1]) && (!current
->need_resched
) )
205 * Mark a page as reserved so we can mess with it
206 * If we're interrupted we keep this page and our place in it
207 * since we validly hold it and it's reserved for us.
209 pageptr
= __get_free_pages(GFP_ATOMIC
, 0);
213 if ( current
->need_resched
)
217 * Make the page no cache so we don't blow our cache with 0's
219 pte
= find_pte(&init_mm
, pageptr
);
222 printk("pte NULL in zero_paged()\n");
227 flush_tlb_page(find_vma(&init_mm
,pageptr
),pageptr
);
229 * Important here to not take time away from real processes.
231 for ( bytecount
= 0; bytecount
< PAGE_SIZE
; bytecount
+= 4 )
233 if ( current
->need_resched
)
235 *(unsigned long *)(bytecount
+ pageptr
) = 0;
239 * If we finished zero-ing out a page add this page to
240 * the zero_cache atomically -- we can't use
241 * down/up since we can't sleep in idle.
242 * Disabling interrupts is also a bad idea since we would
243 * steal time away from real processes.
244 * We can also have several zero_paged's running
245 * on different processors so we can't interfere with them.
246 * So we update the list atomically without locking it.
250 /* turn cache on for this page */
252 flush_tlb_page(find_vma(&init_mm
,pageptr
),pageptr
);
253 /* atomically add this page to the list */
254 asm ( "101:lwarx %0,0,%2\n" /* reserve zero_cache */
255 " stw %0,0(%3)\n" /* update *pageptr */
257 " sync\n" /* let store settle */
259 " stwcx. %3,0,%2\n" /* update zero_cache in mem */
260 " bne- 101b\n" /* if lost reservation try again */
261 : "=&r" (tmp
), "+m" (zero_quicklist
)
262 : "r" (&zero_quicklist
), "r" (pageptr
)
265 * This variable is used in the above loop and nowhere
266 * else so the worst that could happen is we would
267 * zero out one more or one less page than we want
268 * per processor on the machine. This is because
269 * we could add our page to the list but not have
270 * zerocount updated yet when another processor
273 atomic_inc((atomic_t
*)&zero_cache_sz
);
274 atomic_inc((atomic_t
*)&zero_cache_total
);
279 void power_save(void)
281 unsigned long msr
, hid0
;
283 /* only sleep on the 603-family/750 processors */
284 switch (_get_PVR() >> 16) {
292 if (!current
->need_resched
) {
293 asm("mfspr %0,1008" : "=r" (hid0
) :);
294 hid0
&= ~(HID0_NAP
| HID0_SLEEP
| HID0_DOZE
);
295 hid0
|= (powersave_nap
? HID0_NAP
: HID0_DOZE
) | HID0_DPM
;
296 asm("mtspr 1008,%0" : : "r" (hid0
));
298 /* set the POW bit in the MSR, and enable interrupts
299 * so we wake up sometime! */
300 __sti(); /* this keeps rtl from getting confused -- Cort */
301 _nmask_and_or_msr(0, MSR_POW
| MSR_EE
);