/* * Floating proportions * * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> * * Description: * * The floating proportion is a time derivative with an exponentially decaying * history: * * p_{j} = \Sum_{i=0} (dx_{j}/dt_{-i}) / 2^(1+i) * * Where j is an element from {prop_local}, x_{j} is j's number of events, * and i the time period over which the differential is taken. So d/dt_{-i} is * the differential over the i-th last period. * * The decaying history gives smooth transitions. The time differential carries * the notion of speed. * * The denominator is 2^(1+i) because we want the series to be normalised, ie. * * \Sum_{i=0} 1/2^(1+i) = 1 * * Further more, if we measure time (t) in the same events as x; so that: * * t = \Sum_{j} x_{j} * * we get that: * * \Sum_{j} p_{j} = 1 * * Writing this in an iterative fashion we get (dropping the 'd's): * * if (++x_{j}, ++t > period) * t /= 2; * for_each (j) * x_{j} /= 2; * * so that: * * p_{j} = x_{j} / t; * * We optimize away the '/= 2' for the global time delta by noting that: * * if (++t > period) t /= 2: * * Can be approximated by: * * period/2 + (++t % period/2) * * [ Furthermore, when we choose period to be 2^n it can be written in terms of * binary operations and wraparound artefacts disappear. ] * * Also note that this yields a natural counter of the elapsed periods: * * c = t / (period/2) * * [ Its monotonic increasing property can be applied to mitigate the wrap- * around issue. ] * * This allows us to do away with the loop over all prop_locals on each period * expiration. By remembering the period count under which it was last accessed * as c_{j}, we can obtain the number of 'missed' cycles from: * * c - c_{j} * * We can then lazily catch up to the global period count every time we are * going to use x_{j}, by doing: * * x_{j} /= 2^(c - c_{j}), c_{j} = c */ #include <linux/proportions.h> #include <linux/rcupdate.h> int prop_descriptor_init(struct prop_descriptor *pd, int shift) { int err; if (shift > PROP_MAX_SHIFT) shift = PROP_MAX_SHIFT; pd->index = 0; pd->pg[0].shift = shift; mutex_init(&pd->mutex); err = percpu_counter_init(&pd->pg[0].events, 0); if (err) goto out; err = percpu_counter_init(&pd->pg[1].events, 0); if (err) percpu_counter_destroy(&pd->pg[0].events); out: return err; } /* * We have two copies, and flip between them to make it seem like an atomic * update. The update is not really atomic wrt the events counter, but * it is internally consistent with the bit layout depending on shift. * * We copy the events count, move the bits around and flip the index. */ void prop_change_shift(struct prop_descriptor *pd, int shift) { int index; int offset; u64 events; unsigned long flags; if (shift > PROP_MAX_SHIFT) shift = PROP_MAX_SHIFT; mutex_lock(&pd->mutex); index = pd->index ^ 1; offset = pd->pg[pd->index].shift - shift; if (!offset) goto out; pd->pg[index].shift = shift; local_irq_save(flags); events = percpu_counter_sum(&pd->pg[pd->index].events); if (offset < 0) events <<= -offset; else events >>= offset; percpu_counter_set(&pd->pg[index].events, events); /* * ensure the new pg is fully written before the switch */ smp_wmb(); pd->index = index; local_irq_restore(flags); synchronize_rcu(); out: mutex_unlock(&pd->mutex); } /* * wrap the access to the data in an rcu_read_lock() section; * this is used to track the active references. */ static struct prop_global *prop_get_global(struct prop_descriptor *pd) __acquires(RCU) { int index; rcu_read_lock(); index = pd->index; /* * match the wmb from vcd_flip() */ smp_rmb(); return &pd->pg[index]; } static void prop_put_global(struct prop_descriptor *pd, struct prop_global *pg) __releases(RCU) { rcu_read_unlock(); } static void prop_adjust_shift(int *pl_shift, unsigned long *pl_period, int new_shift) { int offset = *pl_shift - new_shift; if (!offset) return; if (offset < 0) *pl_period <<= -offset; else *pl_period >>= offset; *pl_shift = new_shift; } /* * PERCPU */ #define PROP_BATCH (8*(1+ilog2(nr_cpu_ids))) int prop_local_init_percpu(struct prop_local_percpu *pl) { raw_spin_lock_init(&pl->lock); pl->shift = 0; pl->period = 0; return percpu_counter_init(&pl->events, 0); } void prop_local_destroy_percpu(struct prop_local_percpu *pl) { percpu_counter_destroy(&pl->events); } /* * Catch up with missed period expirations. * * until (c_{j} == c) * x_{j} -= x_{j}/2; * c_{j}++; */ static void prop_norm_percpu(struct prop_global *pg, struct prop_local_percpu *pl) { unsigned long period = 1UL << (pg->shift - 1); unsigned long period_mask = ~(period - 1); unsigned long global_period; unsigned long flags; global_period = percpu_counter_read(&pg->events); global_period &= period_mask; /* * Fast path - check if the local and global period count still match * outside of the lock. */ if (pl->period == global_period) return; raw_spin_lock_irqsave(&pl->lock, flags); prop_adjust_shift(&pl->shift, &pl->period, pg->shift); /* * For each missed period, we half the local counter. * basically: * pl->events >> (global_period - pl->period); */ period = (global_period - pl->period) >> (pg->shift - 1); if (period < BITS_PER_LONG) { s64 val = percpu_counter_read(&pl->events); if (val < (nr_cpu_ids * PROP_BATCH)) val = percpu_counter_sum(&pl->events); __percpu_counter_add(&pl->events, -val + @media only all and (prefers-color-scheme: dark) { .highlight .hll { background-color: #49483e } .highlight .c { color: #75715e } /* Comment */ .highlight .err { color: #960050; background-color: #1e0010 } /* Error */ .highlight .k { color: #66d9ef } /* Keyword */ .highlight .l { color: #ae81ff } /* Literal */ .highlight .n { color: #f8f8f2 } /* Name */ .highlight .o { color: #f92672 } /* Operator */ .highlight .p { color: #f8f8f2 } /* Punctuation */ .highlight .ch { color: #75715e } /* Comment.Hashbang */ .highlight .cm { color: #75715e } /* Comment.Multiline */ .highlight .cp { color: #75715e } /* Comment.Preproc */ .highlight .cpf { color: #75715e } /* Comment.PreprocFile */ .highlight .c1 { color: #75715e } /* Comment.Single */ .highlight .cs { color: #75715e } /* Comment.Special */ .highlight .gd { color: #f92672 } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gi { color: #a6e22e } /* Generic.Inserted */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #75715e } /* Generic.Subheading */ .highlight .kc { color: #66d9ef } /* Keyword.Constant */ .highlight .kd { color: #66d9ef } /* Keyword.Declaration */ .highlight .kn { color: #f92672 } /* Keyword.Namespace */ .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */ .highlight .kr { color: #66d9ef } /* Keyword.Reserved */ .highlight .kt { color: #66d9ef } /* Keyword.Type */ .highlight .ld { color: #e6db74 } /* Literal.Date */ .highlight .m { color: #ae81ff } /* Literal.Number */ .highlight .s { color: #e6db74 } /* Literal.String */ .highlight .na { color: #a6e22e } /* Name.Attribute */ .highlight .nb { color: #f8f8f2 } /* Name.Builtin */ .highlight .nc { color: #a6e22e } /* Name.Class */ .highlight .no { color: #66d9ef } /* Name.Constant */ .highlight .nd { color: #a6e22e } /* Name.Decorator */ .highlight .ni { color: #f8f8f2 } /* Name.Entity */ .highlight .ne { color: #a6e22e } /* Name.Exception */ .highlight .nf { color: #a6e22e } /* Name.Function */ .highlight .nl { color: #f8f8f2 } /* Name.Label */ .highlight .nn { color: #f8f8f2 } /* Name.Namespace */ .highlight .nx { color: #a6e22e } /* Name.Other */ .highlight .py { color: #f8f8f2 } /* Name.Property */ .highlight .nt { color: #f92672 } /* Name.Tag */ .highlight .nv { color: #f8f8f2 } /* Name.Variable */ .highlight .ow { color: #f92672 } /* Operator.Word */ .highlight .w { color: #f8f8f2 } /* Text.Whitespace */ .highlight .mb { color: #ae81ff } /* Literal.Number.Bin */ .highlight .mf { color: #ae81ff } /* Literal.Number.Float */ .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */ .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */ .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */ .highlight .sa { color: #e6db74 } /* Literal.String.Affix */ .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */ .highlight .sc { color: #e6db74 } /* Literal.String.Char */ .highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */ .highlight .sd { color: #e6db74 } /* Literal.String.Doc */ .highlight .s2 { color: #e6db74 } /* Literal.String.Double */ .highlight .se { color: #ae81ff } /* Literal.String.Escape */ .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */ .highlight .si { color: #e6db74 } /* Literal.String.Interpol */ .highlight .sx { color: #e6db74 } /* Literal.String.Other */ .highlight .sr { color: #e6db74 } /* Literal.String.Regex */ .highlight .s1 { color: #e6db74 } /* Literal.String.Single */ .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */ .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #a6e22e } /* Name.Function.Magic */ .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */ .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */ .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */ .highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */ .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */ } @media (prefers-color-scheme: light) { .highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ } /* * framebuffer-coreboot.c * * Memory based framebuffer accessed through coreboot table. * * Copyright 2012-2013 David Herrmann <dh.herrmann@gmail.com> * Copyright 2017 Google Inc. * Copyright 2017 Samuel Holland <samuel@sholland.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2.0 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include <linux/device.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/platform_data/simplefb.h> #include <linux/platform_device.h> #include "coreboot_table.h" #define CB_TAG_FRAMEBUFFER 0x12 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS; static int framebuffer_probe(struct coreboot_device *dev) { int i; u32 length; struct lb_framebuffer *fb = &dev->framebuffer; struct platform_device *pdev; struct resource res; struct simplefb_platform_data pdata = { .width = fb->x_resolution, .height = fb->y_resolution, .stride = fb->bytes_per_line, .format = NULL, }; for (i = 0; i < ARRAY_SIZE(formats); ++i) { if (fb->bits_per_pixel == formats[i].bits_per_pixel && fb->red_mask_pos == formats[i].red.offset && fb->red_mask_size == formats[i].red.length && fb->green_mask_pos == formats[i].green.offset && fb->green_mask_size == formats[i].green.length && fb->blue_mask_pos == formats[i].blue.offset && fb->blue_mask_size == formats[i].blue.length && fb->reserved_mask_pos == formats[i].transp.offset && fb->reserved_mask_size == formats[i].transp.length) pdata.format = formats[i].name; } if (!pdata.format) return -ENODEV; memset(&res, 0, sizeof(res)); res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; res.name = "Coreboot Framebuffer"; res.start = fb->physical_address; length = PAGE_ALIGN(fb->y_resolution * fb->bytes_per_line); res.end = res.start + length - 1; if (res.end <= res.start) return -EINVAL; pdev = platform_device_register_resndata(&dev->dev, "simple-framebuffer", 0, &res, 1, &pdata, sizeof(pdata)); if (IS_ERR(pdev)) pr_warn("coreboot: could not register framebuffer\n"); else dev_set_drvdata(&dev->dev, pdev); return PTR_ERR_OR_ZERO(pdev); } static int framebuffer_remove(struct coreboot_device *dev) { struct platform_device *pdev = dev_get_drvdata(&dev->dev); platform_device_unregister(pdev); return 0; } static struct coreboot_driver framebuffer_driver = { .probe = framebuffer_probe, .remove = framebuffer_remove, .drv = { .name = "framebuffer", }, .tag = CB_TAG_FRAMEBUFFER, }; static int __init coreboot_framebuffer_init(void) { return coreboot_driver_register(&framebuffer_driver); } static void coreboot_framebuffer_exit(void) { coreboot_driver_unregister(&framebuffer_driver); } module_init(coreboot_framebuffer_init); module_exit(coreboot_framebuffer_exit); MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>"); MODULE_LICENSE("GPL");
/* * framebuffer-coreboot.c * * Memory based framebuffer accessed through coreboot table. * * Copyright 2012-2013 David Herrmann <dh.herrmann@gmail.com> * Copyright 2017 Google Inc. * Copyright 2017 Samuel Holland <samuel@sholland.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2.0 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #include <linux/device.h> #include <linux/kernel.h> #include <linux/mm.h> #include <linux/module.h> #include <linux/platform_data/simplefb.h> #include <linux/platform_device.h> #include "coreboot_table.h" #define CB_TAG_FRAMEBUFFER 0x12 static const struct simplefb_format formats[] = SIMPLEFB_FORMATS; static int framebuffer_probe(struct coreboot_device *dev) { int i; u32 length; struct lb_framebuffer *fb = &dev->framebuffer; struct platform_device *pdev; struct resource res; struct simplefb_platform_data pdata = { .width = fb->x_resolution, .height = fb->y_resolution, .stride = fb->bytes_per_line, .format = NULL, }; for (i = 0; i < ARRAY_SIZE(formats); ++i) { if (fb->bits_per_pixel == formats[i].bits_per_pixel && fb->red_mask_pos == formats[i].red.offset && fb->red_mask_size == formats[i].red.length && fb->green_mask_pos == formats[i].green.offset && fb->green_mask_size == formats[i].green.length && fb->blue_mask_pos == formats[i].blue.offset && fb->blue_mask_size == formats[i].blue.length && fb->reserved_mask_pos == formats[i].transp.offset && fb->reserved_mask_size == formats[i].transp.length) pdata.format = formats[i].name; } if (!pdata.format) return -ENODEV; memset(&res, 0, sizeof(res)); res.flags = IORESOURCE_MEM | IORESOURCE_BUSY; res.name = "Coreboot Framebuffer"; res.start = fb->physical_address; length = PAGE_ALIGN(fb->y_resolution * fb->bytes_per_line); res.end = res.start + length - 1; if (res.end <= res.start) return -EINVAL; pdev = platform_device_register_resndata(&dev->dev, "simple-framebuffer", 0, &res, 1, &pdata, sizeof(pdata)); if (IS_ERR(pdev)) pr_warn("coreboot: could not register framebuffer\n"); else dev_set_drvdata(&dev->dev, pdev); return PTR_ERR_OR_ZERO(pdev); } static int framebuffer_remove(struct coreboot_device *dev) { struct platform_device *pdev = dev_get_drvdata(&dev->dev); platform_device_unregister(pdev); return 0; } static struct coreboot_driver framebuffer_driver = { .probe = framebuffer_probe, .remove = framebuffer_remove, .drv = { .name = "framebuffer", }, .tag = CB_TAG_FRAMEBUFFER, }; static int __init coreboot_framebuffer_init(void) { return coreboot_driver_register(&framebuffer_driver); } static void coreboot_framebuffer_exit(void) { coreboot_driver_unregister(&framebuffer_driver); } module_init(coreboot_framebuffer_init); module_exit(coreboot_framebuffer_exit); MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>"); MODULE_LICENSE("GPL");