channel.c (f26e8817b235d8764363bffcc9cbfc61867371f2) channel.c (8474b02531c4881a762c52ef869c52429e38633f)
1/*
2 * Tegra host1x Channel
3 *
4 * Copyright (c) 2010-2013, NVIDIA Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.

--- 10 unchanged lines hidden (view full) ---

19#include <linux/slab.h>
20#include <linux/module.h>
21
22#include "channel.h"
23#include "dev.h"
24#include "job.h"
25
26/* Constructor for the host1x device list */
1/*
2 * Tegra host1x Channel
3 *
4 * Copyright (c) 2010-2013, NVIDIA Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.

--- 10 unchanged lines hidden (view full) ---

19#include <linux/slab.h>
20#include <linux/module.h>
21
22#include "channel.h"
23#include "dev.h"
24#include "job.h"
25
26/* Constructor for the host1x device list */
27int host1x_channel_list_init(struct host1x *host)
27int host1x_channel_list_init(struct host1x_channel_list *chlist,
28 unsigned int num_channels)
28{
29{
29 INIT_LIST_HEAD(&host->chlist.list);
30 mutex_init(&host->chlist_mutex);
30 chlist->channels = kcalloc(num_channels, sizeof(struct host1x_channel),
31 GFP_KERNEL);
32 if (!chlist->channels)
33 return -ENOMEM;
31
34
32 if (host->info->nb_channels > BITS_PER_LONG) {
33 WARN(1, "host1x hardware has more channels than supported by the driver\n");
34 return -ENOSYS;
35 chlist->allocated_channels =
36 kcalloc(BITS_TO_LONGS(num_channels), sizeof(unsigned long),
37 GFP_KERNEL);
38 if (!chlist->allocated_channels) {
39 kfree(chlist->channels);
40 return -ENOMEM;
35 }
36
41 }
42
43 bitmap_zero(chlist->allocated_channels, num_channels);
44
37 return 0;
38}
39
45 return 0;
46}
47
48void host1x_channel_list_free(struct host1x_channel_list *chlist)
49{
50 kfree(chlist->allocated_channels);
51 kfree(chlist->channels);
52}
53
40int host1x_job_submit(struct host1x_job *job)
41{
42 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
43
44 return host1x_hw_channel_submit(host, job);
45}
46EXPORT_SYMBOL(host1x_job_submit);
47
48struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
49{
54int host1x_job_submit(struct host1x_job *job)
55{
56 struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
57
58 return host1x_hw_channel_submit(host, job);
59}
60EXPORT_SYMBOL(host1x_job_submit);
61
62struct host1x_channel *host1x_channel_get(struct host1x_channel *channel)
63{
50 int err = 0;
64 kref_get(&channel->refcount);
51
65
52 mutex_lock(&channel->reflock);
66 return channel;
67}
68EXPORT_SYMBOL(host1x_channel_get);
53
69
54 if (channel->refcount == 0)
55 err = host1x_cdma_init(&channel->cdma);
70/**
71 * host1x_channel_get_index() - Attempt to get channel reference by index
72 * @host: Host1x device object
73 * @index: Index of channel
74 *
75 * If channel number @index is currently allocated, increase its refcount
76 * and return a pointer to it. Otherwise, return NULL.
77 */
78struct host1x_channel *host1x_channel_get_index(struct host1x *host,
79 unsigned int index)
80{
81 struct host1x_channel *ch = &host->channel_list.channels[index];
56
82
57 if (!err)
58 channel->refcount++;
83 if (!kref_get_unless_zero(&ch->refcount))
84 return NULL;
59
85
60 mutex_unlock(&channel->reflock);
86 return ch;
87}
61
88
62 return err ? NULL : channel;
89static void release_channel(struct kref *kref)
90{
91 struct host1x_channel *channel =
92 container_of(kref, struct host1x_channel, refcount);
93 struct host1x *host = dev_get_drvdata(channel->dev->parent);
94 struct host1x_channel_list *chlist = &host->channel_list;
95
96 host1x_hw_cdma_stop(host, &channel->cdma);
97 host1x_cdma_deinit(&channel->cdma);
98
99 clear_bit(channel->id, chlist->allocated_channels);
63}
100}
64EXPORT_SYMBOL(host1x_channel_get);
65
66void host1x_channel_put(struct host1x_channel *channel)
67{
101
102void host1x_channel_put(struct host1x_channel *channel)
103{
68 mutex_lock(&channel->reflock);
104 kref_put(&channel->refcount, release_channel);
105}
106EXPORT_SYMBOL(host1x_channel_put);
69
107
70 if (channel->refcount == 1) {
71 struct host1x *host = dev_get_drvdata(channel->dev->parent);
108static struct host1x_channel *acquire_unused_channel(struct host1x *host)
109{
110 struct host1x_channel_list *chlist = &host->channel_list;
111 unsigned int max_channels = host->info->nb_channels;
112 unsigned int index;
72
113
73 host1x_hw_cdma_stop(host, &channel->cdma);
74 host1x_cdma_deinit(&channel->cdma);
114 index = find_first_zero_bit(chlist->allocated_channels, max_channels);
115 if (index >= max_channels) {
116 dev_err(host->dev, "failed to find free channel\n");
117 return NULL;
75 }
76
118 }
119
77 channel->refcount--;
120 chlist->channels[index].id = index;
78
121
79 mutex_unlock(&channel->reflock);
122 set_bit(index, chlist->allocated_channels);
123
124 return &chlist->channels[index];
80}
125}
81EXPORT_SYMBOL(host1x_channel_put);
82
126
127/**
128 * host1x_channel_request() - Allocate a channel
129 * @device: Host1x unit this channel will be used to send commands to
130 *
131 * Allocates a new host1x channel for @device. If there are no free channels,
132 * this will sleep until one becomes available. May return NULL if CDMA
133 * initialization fails.
134 */
83struct host1x_channel *host1x_channel_request(struct device *dev)
84{
85 struct host1x *host = dev_get_drvdata(dev->parent);
135struct host1x_channel *host1x_channel_request(struct device *dev)
136{
137 struct host1x *host = dev_get_drvdata(dev->parent);
86 unsigned int max_channels = host->info->nb_channels;
87 struct host1x_channel *channel = NULL;
88 unsigned long index;
138 struct host1x_channel_list *chlist = &host->channel_list;
139 struct host1x_channel *channel;
89 int err;
90
140 int err;
141
91 mutex_lock(&host->chlist_mutex);
142 channel = acquire_unused_channel(host);
143 if (!channel)
144 return NULL;
92
145
93 index = find_first_zero_bit(&host->allocated_channels, max_channels);
94 if (index >= max_channels)
95 goto fail;
146 kref_init(&channel->refcount);
147 mutex_init(&channel->submitlock);
148 channel->dev = dev;
96
149
97 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
98 if (!channel)
150 err = host1x_hw_channel_init(host, channel, channel->id);
151 if (err < 0)
99 goto fail;
100
152 goto fail;
153
101 err = host1x_hw_channel_init(host, channel, index);
154 err = host1x_cdma_init(&channel->cdma);
102 if (err < 0)
103 goto fail;
104
155 if (err < 0)
156 goto fail;
157
105 /* Link device to host1x_channel */
106 channel->dev = dev;
107
108 /* Add to channel list */
109 list_add_tail(&channel->list, &host->chlist.list);
110
111 host->allocated_channels |= BIT(index);
112
113 mutex_unlock(&host->chlist_mutex);
114 return channel;
115
116fail:
158 return channel;
159
160fail:
117 dev_err(dev, "failed to init channel\n");
118 kfree(channel);
119 mutex_unlock(&host->chlist_mutex);
120 return NULL;
121}
122EXPORT_SYMBOL(host1x_channel_request);
161 clear_bit(channel->id, chlist->allocated_channels);
123
162
124void host1x_channel_free(struct host1x_channel *channel)
125{
126 struct host1x *host = dev_get_drvdata(channel->dev->parent);
163 dev_err(dev, "failed to initialize channel\n");
127
164
128 host->allocated_channels &= ~BIT(channel->id);
129 list_del(&channel->list);
130 kfree(channel);
165 return NULL;
131}
166}
132EXPORT_SYMBOL(host1x_channel_free);
167EXPORT_SYMBOL(host1x_channel_request);