i915_gem.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/oom.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  41. bool force);
  42. static __must_check int
  43. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  44. bool readonly);
  45. static void
  46. i915_gem_object_retire(struct drm_i915_gem_object *obj);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static unsigned long i915_gem_shrinker_count(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static unsigned long i915_gem_shrinker_scan(struct shrinker *shrinker,
  55. struct shrink_control *sc);
  56. static int i915_gem_shrinker_oom(struct notifier_block *nb,
  57. unsigned long event,
  58. void *ptr);
  59. static unsigned long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  60. static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  61. static bool cpu_cache_is_coherent(struct drm_device *dev,
  62. enum i915_cache_level level)
  63. {
  64. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  65. }
  66. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  67. {
  68. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  69. return true;
  70. return obj->pin_display;
  71. }
  72. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  73. {
  74. if (obj->tiling_mode)
  75. i915_gem_release_mmap(obj);
  76. /* As we do not have an associated fence register, we will force
  77. * a tiling change if we ever need to acquire one.
  78. */
  79. obj->fence_dirty = false;
  80. obj->fence_reg = I915_FENCE_REG_NONE;
  81. }
  82. /* some bookkeeping */
  83. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  84. size_t size)
  85. {
  86. spin_lock(&dev_priv->mm.object_stat_lock);
  87. dev_priv->mm.object_count++;
  88. dev_priv->mm.object_memory += size;
  89. spin_unlock(&dev_priv->mm.object_stat_lock);
  90. }
  91. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  92. size_t size)
  93. {
  94. spin_lock(&dev_priv->mm.object_stat_lock);
  95. dev_priv->mm.object_count--;
  96. dev_priv->mm.object_memory -= size;
  97. spin_unlock(&dev_priv->mm.object_stat_lock);
  98. }
  99. static int
  100. i915_gem_wait_for_error(struct i915_gpu_error *error)
  101. {
  102. int ret;
  103. #define EXIT_COND (!i915_reset_in_progress(error) || \
  104. i915_terminally_wedged(error))
  105. if (EXIT_COND)
  106. return 0;
  107. /*
  108. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  109. * userspace. If it takes that long something really bad is going on and
  110. * we should simply try to bail out and fail as gracefully as possible.
  111. */
  112. ret = wait_event_interruptible_timeout(error->reset_queue,
  113. EXIT_COND,
  114. 10*HZ);
  115. if (ret == 0) {
  116. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  117. return -EIO;
  118. } else if (ret < 0) {
  119. return ret;
  120. }
  121. #undef EXIT_COND
  122. return 0;
  123. }
  124. int i915_mutex_lock_interruptible(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. int ret;
  128. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  129. if (ret)
  130. return ret;
  131. ret = mutex_lock_interruptible(&dev->struct_mutex);
  132. if (ret)
  133. return ret;
  134. WARN_ON(i915_verify_lists(dev));
  135. return 0;
  136. }
  137. static inline bool
  138. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  139. {
  140. return i915_gem_obj_bound_any(obj) && !obj->active;
  141. }
  142. int
  143. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  144. struct drm_file *file)
  145. {
  146. struct drm_i915_private *dev_priv = dev->dev_private;
  147. struct drm_i915_gem_init *args = data;
  148. if (drm_core_check_feature(dev, DRIVER_MODESET))
  149. return -ENODEV;
  150. if (args->gtt_start >= args->gtt_end ||
  151. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  152. return -EINVAL;
  153. /* GEM with user mode setting was never supported on ilk and later. */
  154. if (INTEL_INFO(dev)->gen >= 5)
  155. return -ENODEV;
  156. mutex_lock(&dev->struct_mutex);
  157. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  158. args->gtt_end);
  159. dev_priv->gtt.mappable_end = args->gtt_end;
  160. mutex_unlock(&dev->struct_mutex);
  161. return 0;
  162. }
  163. int
  164. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  165. struct drm_file *file)
  166. {
  167. struct drm_i915_private *dev_priv = dev->dev_private;
  168. struct drm_i915_gem_get_aperture *args = data;
  169. struct drm_i915_gem_object *obj;
  170. size_t pinned;
  171. pinned = 0;
  172. mutex_lock(&dev->struct_mutex);
  173. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  174. if (i915_gem_obj_is_pinned(obj))
  175. pinned += i915_gem_obj_ggtt_size(obj);
  176. mutex_unlock(&dev->struct_mutex);
  177. args->aper_size = dev_priv->gtt.base.total;
  178. args->aper_available_size = args->aper_size - pinned;
  179. return 0;
  180. }
  181. static void i915_gem_object_detach_phys(struct drm_i915_gem_object *obj)
  182. {
  183. drm_dma_handle_t *phys = obj->phys_handle;
  184. if (!phys)
  185. return;
  186. if (obj->madv == I915_MADV_WILLNEED) {
  187. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  188. char *vaddr = phys->vaddr;
  189. int i;
  190. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  191. struct page *page = shmem_read_mapping_page(mapping, i);
  192. if (!IS_ERR(page)) {
  193. char *dst = kmap_atomic(page);
  194. memcpy(dst, vaddr, PAGE_SIZE);
  195. drm_clflush_virt_range(dst, PAGE_SIZE);
  196. kunmap_atomic(dst);
  197. set_page_dirty(page);
  198. mark_page_accessed(page);
  199. page_cache_release(page);
  200. }
  201. vaddr += PAGE_SIZE;
  202. }
  203. i915_gem_chipset_flush(obj->base.dev);
  204. }
  205. #ifdef CONFIG_X86
  206. set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
  207. #endif
  208. drm_pci_free(obj->base.dev, phys);
  209. obj->phys_handle = NULL;
  210. }
  211. int
  212. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  213. int align)
  214. {
  215. drm_dma_handle_t *phys;
  216. struct address_space *mapping;
  217. char *vaddr;
  218. int i;
  219. if (obj->phys_handle) {
  220. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  221. return -EBUSY;
  222. return 0;
  223. }
  224. if (obj->madv != I915_MADV_WILLNEED)
  225. return -EFAULT;
  226. if (obj->base.filp == NULL)
  227. return -EINVAL;
  228. /* create a new object */
  229. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  230. if (!phys)
  231. return -ENOMEM;
  232. vaddr = phys->vaddr;
  233. #ifdef CONFIG_X86
  234. set_memory_wc((unsigned long)vaddr, phys->size / PAGE_SIZE);
  235. #endif
  236. mapping = file_inode(obj->base.filp)->i_mapping;
  237. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  238. struct page *page;
  239. char *src;
  240. page = shmem_read_mapping_page(mapping, i);
  241. if (IS_ERR(page)) {
  242. #ifdef CONFIG_X86
  243. set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
  244. #endif
  245. drm_pci_free(obj->base.dev, phys);
  246. return PTR_ERR(page);
  247. }
  248. src = kmap_atomic(page);
  249. memcpy(vaddr, src, PAGE_SIZE);
  250. kunmap_atomic(src);
  251. mark_page_accessed(page);
  252. page_cache_release(page);
  253. vaddr += PAGE_SIZE;
  254. }
  255. obj->phys_handle = phys;
  256. return 0;
  257. }
  258. static int
  259. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  260. struct drm_i915_gem_pwrite *args,
  261. struct drm_file *file_priv)
  262. {
  263. struct drm_device *dev = obj->base.dev;
  264. void *vaddr = obj->phys_handle->vaddr + args->offset;
  265. char __user *user_data = to_user_ptr(args->data_ptr);
  266. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  267. unsigned long unwritten;
  268. /* The physical object once assigned is fixed for the lifetime
  269. * of the obj, so we can safely drop the lock and continue
  270. * to access vaddr.
  271. */
  272. mutex_unlock(&dev->struct_mutex);
  273. unwritten = copy_from_user(vaddr, user_data, args->size);
  274. mutex_lock(&dev->struct_mutex);
  275. if (unwritten)
  276. return -EFAULT;
  277. }
  278. i915_gem_chipset_flush(dev);
  279. return 0;
  280. }
  281. void *i915_gem_object_alloc(struct drm_device *dev)
  282. {
  283. struct drm_i915_private *dev_priv = dev->dev_private;
  284. return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
  285. }
  286. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  287. {
  288. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  289. kmem_cache_free(dev_priv->slab, obj);
  290. }
  291. static int
  292. i915_gem_create(struct drm_file *file,
  293. struct drm_device *dev,
  294. uint64_t size,
  295. uint32_t *handle_p)
  296. {
  297. struct drm_i915_gem_object *obj;
  298. int ret;
  299. u32 handle;
  300. size = roundup(size, PAGE_SIZE);
  301. if (size == 0)
  302. return -EINVAL;
  303. /* Allocate the new object */
  304. obj = i915_gem_alloc_object(dev, size);
  305. if (obj == NULL)
  306. return -ENOMEM;
  307. ret = drm_gem_handle_create(file, &obj->base, &handle);
  308. /* drop reference from allocate - handle holds it now */
  309. drm_gem_object_unreference_unlocked(&obj->base);
  310. if (ret)
  311. return ret;
  312. *handle_p = handle;
  313. return 0;
  314. }
  315. int
  316. i915_gem_dumb_create(struct drm_file *file,
  317. struct drm_device *dev,
  318. struct drm_mode_create_dumb *args)
  319. {
  320. /* have to work out size/pitch and return them */
  321. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  322. args->size = args->pitch * args->height;
  323. return i915_gem_create(file, dev,
  324. args->size, &args->handle);
  325. }
  326. /**
  327. * Creates a new mm object and returns a handle to it.
  328. */
  329. int
  330. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  331. struct drm_file *file)
  332. {
  333. struct drm_i915_gem_create *args = data;
  334. return i915_gem_create(file, dev,
  335. args->size, &args->handle);
  336. }
  337. static inline int
  338. __copy_to_user_swizzled(char __user *cpu_vaddr,
  339. const char *gpu_vaddr, int gpu_offset,
  340. int length)
  341. {
  342. int ret, cpu_offset = 0;
  343. while (length > 0) {
  344. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  345. int this_length = min(cacheline_end - gpu_offset, length);
  346. int swizzled_gpu_offset = gpu_offset ^ 64;
  347. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  348. gpu_vaddr + swizzled_gpu_offset,
  349. this_length);
  350. if (ret)
  351. return ret + length;
  352. cpu_offset += this_length;
  353. gpu_offset += this_length;
  354. length -= this_length;
  355. }
  356. return 0;
  357. }
  358. static inline int
  359. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  360. const char __user *cpu_vaddr,
  361. int length)
  362. {
  363. int ret, cpu_offset = 0;
  364. while (length > 0) {
  365. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  366. int this_length = min(cacheline_end - gpu_offset, length);
  367. int swizzled_gpu_offset = gpu_offset ^ 64;
  368. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  369. cpu_vaddr + cpu_offset,
  370. this_length);
  371. if (ret)
  372. return ret + length;
  373. cpu_offset += this_length;
  374. gpu_offset += this_length;
  375. length -= this_length;
  376. }
  377. return 0;
  378. }
  379. /*
  380. * Pins the specified object's pages and synchronizes the object with
  381. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  382. * flush the object from the CPU cache.
  383. */
  384. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  385. int *needs_clflush)
  386. {
  387. int ret;
  388. *needs_clflush = 0;
  389. if (!obj->base.filp)
  390. return -EINVAL;
  391. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  392. /* If we're not in the cpu read domain, set ourself into the gtt
  393. * read domain and manually flush cachelines (if required). This
  394. * optimizes for the case when the gpu will dirty the data
  395. * anyway again before the next pread happens. */
  396. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  397. obj->cache_level);
  398. ret = i915_gem_object_wait_rendering(obj, true);
  399. if (ret)
  400. return ret;
  401. i915_gem_object_retire(obj);
  402. }
  403. ret = i915_gem_object_get_pages(obj);
  404. if (ret)
  405. return ret;
  406. i915_gem_object_pin_pages(obj);
  407. return ret;
  408. }
  409. /* Per-page copy function for the shmem pread fastpath.
  410. * Flushes invalid cachelines before reading the target if
  411. * needs_clflush is set. */
  412. static int
  413. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  414. char __user *user_data,
  415. bool page_do_bit17_swizzling, bool needs_clflush)
  416. {
  417. char *vaddr;
  418. int ret;
  419. if (unlikely(page_do_bit17_swizzling))
  420. return -EINVAL;
  421. vaddr = kmap_atomic(page);
  422. if (needs_clflush)
  423. drm_clflush_virt_range(vaddr + shmem_page_offset,
  424. page_length);
  425. ret = __copy_to_user_inatomic(user_data,
  426. vaddr + shmem_page_offset,
  427. page_length);
  428. kunmap_atomic(vaddr);
  429. return ret ? -EFAULT : 0;
  430. }
  431. static void
  432. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  433. bool swizzled)
  434. {
  435. if (unlikely(swizzled)) {
  436. unsigned long start = (unsigned long) addr;
  437. unsigned long end = (unsigned long) addr + length;
  438. /* For swizzling simply ensure that we always flush both
  439. * channels. Lame, but simple and it works. Swizzled
  440. * pwrite/pread is far from a hotpath - current userspace
  441. * doesn't use it at all. */
  442. start = round_down(start, 128);
  443. end = round_up(end, 128);
  444. drm_clflush_virt_range((void *)start, end - start);
  445. } else {
  446. drm_clflush_virt_range(addr, length);
  447. }
  448. }
  449. /* Only difference to the fast-path function is that this can handle bit17
  450. * and uses non-atomic copy and kmap functions. */
  451. static int
  452. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  453. char __user *user_data,
  454. bool page_do_bit17_swizzling, bool needs_clflush)
  455. {
  456. char *vaddr;
  457. int ret;
  458. vaddr = kmap(page);
  459. if (needs_clflush)
  460. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  461. page_length,
  462. page_do_bit17_swizzling);
  463. if (page_do_bit17_swizzling)
  464. ret = __copy_to_user_swizzled(user_data,
  465. vaddr, shmem_page_offset,
  466. page_length);
  467. else
  468. ret = __copy_to_user(user_data,
  469. vaddr + shmem_page_offset,
  470. page_length);
  471. kunmap(page);
  472. return ret ? - EFAULT : 0;
  473. }
  474. static int
  475. i915_gem_shmem_pread(struct drm_device *dev,
  476. struct drm_i915_gem_object *obj,
  477. struct drm_i915_gem_pread *args,
  478. struct drm_file *file)
  479. {
  480. char __user *user_data;
  481. ssize_t remain;
  482. loff_t offset;
  483. int shmem_page_offset, page_length, ret = 0;
  484. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  485. int prefaulted = 0;
  486. int needs_clflush = 0;
  487. struct sg_page_iter sg_iter;
  488. user_data = to_user_ptr(args->data_ptr);
  489. remain = args->size;
  490. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  491. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  492. if (ret)
  493. return ret;
  494. offset = args->offset;
  495. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  496. offset >> PAGE_SHIFT) {
  497. struct page *page = sg_page_iter_page(&sg_iter);
  498. if (remain <= 0)
  499. break;
  500. /* Operation in this page
  501. *
  502. * shmem_page_offset = offset within page in shmem file
  503. * page_length = bytes to copy for this page
  504. */
  505. shmem_page_offset = offset_in_page(offset);
  506. page_length = remain;
  507. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  508. page_length = PAGE_SIZE - shmem_page_offset;
  509. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  510. (page_to_phys(page) & (1 << 17)) != 0;
  511. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  512. user_data, page_do_bit17_swizzling,
  513. needs_clflush);
  514. if (ret == 0)
  515. goto next_page;
  516. mutex_unlock(&dev->struct_mutex);
  517. if (likely(!i915.prefault_disable) && !prefaulted) {
  518. ret = fault_in_multipages_writeable(user_data, remain);
  519. /* Userspace is tricking us, but we've already clobbered
  520. * its pages with the prefault and promised to write the
  521. * data up to the first fault. Hence ignore any errors
  522. * and just continue. */
  523. (void)ret;
  524. prefaulted = 1;
  525. }
  526. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  527. user_data, page_do_bit17_swizzling,
  528. needs_clflush);
  529. mutex_lock(&dev->struct_mutex);
  530. if (ret)
  531. goto out;
  532. next_page:
  533. remain -= page_length;
  534. user_data += page_length;
  535. offset += page_length;
  536. }
  537. out:
  538. i915_gem_object_unpin_pages(obj);
  539. return ret;
  540. }
  541. /**
  542. * Reads data from the object referenced by handle.
  543. *
  544. * On error, the contents of *data are undefined.
  545. */
  546. int
  547. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  548. struct drm_file *file)
  549. {
  550. struct drm_i915_gem_pread *args = data;
  551. struct drm_i915_gem_object *obj;
  552. int ret = 0;
  553. if (args->size == 0)
  554. return 0;
  555. if (!access_ok(VERIFY_WRITE,
  556. to_user_ptr(args->data_ptr),
  557. args->size))
  558. return -EFAULT;
  559. ret = i915_mutex_lock_interruptible(dev);
  560. if (ret)
  561. return ret;
  562. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  563. if (&obj->base == NULL) {
  564. ret = -ENOENT;
  565. goto unlock;
  566. }
  567. /* Bounds check source. */
  568. if (args->offset > obj->base.size ||
  569. args->size > obj->base.size - args->offset) {
  570. ret = -EINVAL;
  571. goto out;
  572. }
  573. /* prime objects have no backing filp to GEM pread/pwrite
  574. * pages from.
  575. */
  576. if (!obj->base.filp) {
  577. ret = -EINVAL;
  578. goto out;
  579. }
  580. trace_i915_gem_object_pread(obj, args->offset, args->size);
  581. ret = i915_gem_shmem_pread(dev, obj, args, file);
  582. out:
  583. drm_gem_object_unreference(&obj->base);
  584. unlock:
  585. mutex_unlock(&dev->struct_mutex);
  586. return ret;
  587. }
  588. /* This is the fast write path which cannot handle
  589. * page faults in the source data
  590. */
  591. static inline int
  592. fast_user_write(struct io_mapping *mapping,
  593. loff_t page_base, int page_offset,
  594. char __user *user_data,
  595. int length)
  596. {
  597. void __iomem *vaddr_atomic;
  598. void *vaddr;
  599. unsigned long unwritten;
  600. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  601. /* We can use the cpu mem copy function because this is X86. */
  602. vaddr = (void __force*)vaddr_atomic + page_offset;
  603. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  604. user_data, length);
  605. io_mapping_unmap_atomic(vaddr_atomic);
  606. return unwritten;
  607. }
  608. /**
  609. * This is the fast pwrite path, where we copy the data directly from the
  610. * user into the GTT, uncached.
  611. */
  612. static int
  613. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  614. struct drm_i915_gem_object *obj,
  615. struct drm_i915_gem_pwrite *args,
  616. struct drm_file *file)
  617. {
  618. struct drm_i915_private *dev_priv = dev->dev_private;
  619. ssize_t remain;
  620. loff_t offset, page_base;
  621. char __user *user_data;
  622. int page_offset, page_length, ret;
  623. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  624. if (ret)
  625. goto out;
  626. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  627. if (ret)
  628. goto out_unpin;
  629. ret = i915_gem_object_put_fence(obj);
  630. if (ret)
  631. goto out_unpin;
  632. user_data = to_user_ptr(args->data_ptr);
  633. remain = args->size;
  634. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  635. while (remain > 0) {
  636. /* Operation in this page
  637. *
  638. * page_base = page offset within aperture
  639. * page_offset = offset within page
  640. * page_length = bytes to copy for this page
  641. */
  642. page_base = offset & PAGE_MASK;
  643. page_offset = offset_in_page(offset);
  644. page_length = remain;
  645. if ((page_offset + remain) > PAGE_SIZE)
  646. page_length = PAGE_SIZE - page_offset;
  647. /* If we get a fault while copying data, then (presumably) our
  648. * source page isn't available. Return the error and we'll
  649. * retry in the slow path.
  650. */
  651. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  652. page_offset, user_data, page_length)) {
  653. ret = -EFAULT;
  654. goto out_unpin;
  655. }
  656. remain -= page_length;
  657. user_data += page_length;
  658. offset += page_length;
  659. }
  660. out_unpin:
  661. i915_gem_object_ggtt_unpin(obj);
  662. out:
  663. return ret;
  664. }
  665. /* Per-page copy function for the shmem pwrite fastpath.
  666. * Flushes invalid cachelines before writing to the target if
  667. * needs_clflush_before is set and flushes out any written cachelines after
  668. * writing if needs_clflush is set. */
  669. static int
  670. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  671. char __user *user_data,
  672. bool page_do_bit17_swizzling,
  673. bool needs_clflush_before,
  674. bool needs_clflush_after)
  675. {
  676. char *vaddr;
  677. int ret;
  678. if (unlikely(page_do_bit17_swizzling))
  679. return -EINVAL;
  680. vaddr = kmap_atomic(page);
  681. if (needs_clflush_before)
  682. drm_clflush_virt_range(vaddr + shmem_page_offset,
  683. page_length);
  684. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  685. user_data, page_length);
  686. if (needs_clflush_after)
  687. drm_clflush_virt_range(vaddr + shmem_page_offset,
  688. page_length);
  689. kunmap_atomic(vaddr);
  690. return ret ? -EFAULT : 0;
  691. }
  692. /* Only difference to the fast-path function is that this can handle bit17
  693. * and uses non-atomic copy and kmap functions. */
  694. static int
  695. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  696. char __user *user_data,
  697. bool page_do_bit17_swizzling,
  698. bool needs_clflush_before,
  699. bool needs_clflush_after)
  700. {
  701. char *vaddr;
  702. int ret;
  703. vaddr = kmap(page);
  704. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  705. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  706. page_length,
  707. page_do_bit17_swizzling);
  708. if (page_do_bit17_swizzling)
  709. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  710. user_data,
  711. page_length);
  712. else
  713. ret = __copy_from_user(vaddr + shmem_page_offset,
  714. user_data,
  715. page_length);
  716. if (needs_clflush_after)
  717. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  718. page_length,
  719. page_do_bit17_swizzling);
  720. kunmap(page);
  721. return ret ? -EFAULT : 0;
  722. }
  723. static int
  724. i915_gem_shmem_pwrite(struct drm_device *dev,
  725. struct drm_i915_gem_object *obj,
  726. struct drm_i915_gem_pwrite *args,
  727. struct drm_file *file)
  728. {
  729. ssize_t remain;
  730. loff_t offset;
  731. char __user *user_data;
  732. int shmem_page_offset, page_length, ret = 0;
  733. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  734. int hit_slowpath = 0;
  735. int needs_clflush_after = 0;
  736. int needs_clflush_before = 0;
  737. struct sg_page_iter sg_iter;
  738. user_data = to_user_ptr(args->data_ptr);
  739. remain = args->size;
  740. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  741. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  742. /* If we're not in the cpu write domain, set ourself into the gtt
  743. * write domain and manually flush cachelines (if required). This
  744. * optimizes for the case when the gpu will use the data
  745. * right away and we therefore have to clflush anyway. */
  746. needs_clflush_after = cpu_write_needs_clflush(obj);
  747. ret = i915_gem_object_wait_rendering(obj, false);
  748. if (ret)
  749. return ret;
  750. i915_gem_object_retire(obj);
  751. }
  752. /* Same trick applies to invalidate partially written cachelines read
  753. * before writing. */
  754. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  755. needs_clflush_before =
  756. !cpu_cache_is_coherent(dev, obj->cache_level);
  757. ret = i915_gem_object_get_pages(obj);
  758. if (ret)
  759. return ret;
  760. i915_gem_object_pin_pages(obj);
  761. offset = args->offset;
  762. obj->dirty = 1;
  763. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  764. offset >> PAGE_SHIFT) {
  765. struct page *page = sg_page_iter_page(&sg_iter);
  766. int partial_cacheline_write;
  767. if (remain <= 0)
  768. break;
  769. /* Operation in this page
  770. *
  771. * shmem_page_offset = offset within page in shmem file
  772. * page_length = bytes to copy for this page
  773. */
  774. shmem_page_offset = offset_in_page(offset);
  775. page_length = remain;
  776. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  777. page_length = PAGE_SIZE - shmem_page_offset;
  778. /* If we don't overwrite a cacheline completely we need to be
  779. * careful to have up-to-date data by first clflushing. Don't
  780. * overcomplicate things and flush the entire patch. */
  781. partial_cacheline_write = needs_clflush_before &&
  782. ((shmem_page_offset | page_length)
  783. & (boot_cpu_data.x86_clflush_size - 1));
  784. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  785. (page_to_phys(page) & (1 << 17)) != 0;
  786. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  787. user_data, page_do_bit17_swizzling,
  788. partial_cacheline_write,
  789. needs_clflush_after);
  790. if (ret == 0)
  791. goto next_page;
  792. hit_slowpath = 1;
  793. mutex_unlock(&dev->struct_mutex);
  794. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  795. user_data, page_do_bit17_swizzling,
  796. partial_cacheline_write,
  797. needs_clflush_after);
  798. mutex_lock(&dev->struct_mutex);
  799. if (ret)
  800. goto out;
  801. next_page:
  802. remain -= page_length;
  803. user_data += page_length;
  804. offset += page_length;
  805. }
  806. out:
  807. i915_gem_object_unpin_pages(obj);
  808. if (hit_slowpath) {
  809. /*
  810. * Fixup: Flush cpu caches in case we didn't flush the dirty
  811. * cachelines in-line while writing and the object moved
  812. * out of the cpu write domain while we've dropped the lock.
  813. */
  814. if (!needs_clflush_after &&
  815. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  816. if (i915_gem_clflush_object(obj, obj->pin_display))
  817. i915_gem_chipset_flush(dev);
  818. }
  819. }
  820. if (needs_clflush_after)
  821. i915_gem_chipset_flush(dev);
  822. return ret;
  823. }
  824. /**
  825. * Writes data to the object referenced by handle.
  826. *
  827. * On error, the contents of the buffer that were to be modified are undefined.
  828. */
  829. int
  830. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  831. struct drm_file *file)
  832. {
  833. struct drm_i915_gem_pwrite *args = data;
  834. struct drm_i915_gem_object *obj;
  835. int ret;
  836. if (args->size == 0)
  837. return 0;
  838. if (!access_ok(VERIFY_READ,
  839. to_user_ptr(args->data_ptr),
  840. args->size))
  841. return -EFAULT;
  842. if (likely(!i915.prefault_disable)) {
  843. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  844. args->size);
  845. if (ret)
  846. return -EFAULT;
  847. }
  848. ret = i915_mutex_lock_interruptible(dev);
  849. if (ret)
  850. return ret;
  851. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  852. if (&obj->base == NULL) {
  853. ret = -ENOENT;
  854. goto unlock;
  855. }
  856. /* Bounds check destination. */
  857. if (args->offset > obj->base.size ||
  858. args->size > obj->base.size - args->offset) {
  859. ret = -EINVAL;
  860. goto out;
  861. }
  862. /* prime objects have no backing filp to GEM pread/pwrite
  863. * pages from.
  864. */
  865. if (!obj->base.filp) {
  866. ret = -EINVAL;
  867. goto out;
  868. }
  869. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  870. ret = -EFAULT;
  871. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  872. * it would end up going through the fenced access, and we'll get
  873. * different detiling behavior between reading and writing.
  874. * pread/pwrite currently are reading and writing from the CPU
  875. * perspective, requiring manual detiling by the client.
  876. */
  877. if (obj->phys_handle) {
  878. ret = i915_gem_phys_pwrite(obj, args, file);
  879. goto out;
  880. }
  881. if (obj->tiling_mode == I915_TILING_NONE &&
  882. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  883. cpu_write_needs_clflush(obj)) {
  884. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  885. /* Note that the gtt paths might fail with non-page-backed user
  886. * pointers (e.g. gtt mappings when moving data between
  887. * textures). Fallback to the shmem path in that case. */
  888. }
  889. if (ret == -EFAULT || ret == -ENOSPC)
  890. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  891. out:
  892. drm_gem_object_unreference(&obj->base);
  893. unlock:
  894. mutex_unlock(&dev->struct_mutex);
  895. return ret;
  896. }
  897. int
  898. i915_gem_check_wedge(struct i915_gpu_error *error,
  899. bool interruptible)
  900. {
  901. if (i915_reset_in_progress(error)) {
  902. /* Non-interruptible callers can't handle -EAGAIN, hence return
  903. * -EIO unconditionally for these. */
  904. if (!interruptible)
  905. return -EIO;
  906. /* Recovery complete, but the reset failed ... */
  907. if (i915_terminally_wedged(error))
  908. return -EIO;
  909. return -EAGAIN;
  910. }
  911. return 0;
  912. }
  913. /*
  914. * Compare seqno against outstanding lazy request. Emit a request if they are
  915. * equal.
  916. */
  917. int
  918. i915_gem_check_olr(struct intel_engine_cs *ring, u32 seqno)
  919. {
  920. int ret;
  921. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  922. ret = 0;
  923. if (seqno == ring->outstanding_lazy_seqno)
  924. ret = i915_add_request(ring, NULL);
  925. return ret;
  926. }
  927. static void fake_irq(unsigned long data)
  928. {
  929. wake_up_process((struct task_struct *)data);
  930. }
  931. static bool missed_irq(struct drm_i915_private *dev_priv,
  932. struct intel_engine_cs *ring)
  933. {
  934. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  935. }
  936. static bool can_wait_boost(struct drm_i915_file_private *file_priv)
  937. {
  938. if (file_priv == NULL)
  939. return true;
  940. return !atomic_xchg(&file_priv->rps_wait_boost, true);
  941. }
  942. /**
  943. * __wait_seqno - wait until execution of seqno has finished
  944. * @ring: the ring expected to report seqno
  945. * @seqno: duh!
  946. * @reset_counter: reset sequence associated with the given seqno
  947. * @interruptible: do an interruptible wait (normally yes)
  948. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  949. *
  950. * Note: It is of utmost importance that the passed in seqno and reset_counter
  951. * values have been read by the caller in an smp safe manner. Where read-side
  952. * locks are involved, it is sufficient to read the reset_counter before
  953. * unlocking the lock that protects the seqno. For lockless tricks, the
  954. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  955. * inserted.
  956. *
  957. * Returns 0 if the seqno was found within the alloted time. Else returns the
  958. * errno with remaining time filled in timeout argument.
  959. */
  960. static int __wait_seqno(struct intel_engine_cs *ring, u32 seqno,
  961. unsigned reset_counter,
  962. bool interruptible,
  963. struct timespec *timeout,
  964. struct drm_i915_file_private *file_priv)
  965. {
  966. struct drm_device *dev = ring->dev;
  967. struct drm_i915_private *dev_priv = dev->dev_private;
  968. const bool irq_test_in_progress =
  969. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  970. struct timespec before, now;
  971. DEFINE_WAIT(wait);
  972. unsigned long timeout_expire;
  973. int ret;
  974. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  975. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  976. return 0;
  977. timeout_expire = timeout ? jiffies + timespec_to_jiffies_timeout(timeout) : 0;
  978. if (INTEL_INFO(dev)->gen >= 6 && ring->id == RCS && can_wait_boost(file_priv)) {
  979. gen6_rps_boost(dev_priv);
  980. if (file_priv)
  981. mod_delayed_work(dev_priv->wq,
  982. &file_priv->mm.idle_work,
  983. msecs_to_jiffies(100));
  984. }
  985. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring)))
  986. return -ENODEV;
  987. /* Record current time in case interrupted by signal, or wedged */
  988. trace_i915_gem_request_wait_begin(ring, seqno);
  989. getrawmonotonic(&before);
  990. for (;;) {
  991. struct timer_list timer;
  992. prepare_to_wait(&ring->irq_queue, &wait,
  993. interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  994. /* We need to check whether any gpu reset happened in between
  995. * the caller grabbing the seqno and now ... */
  996. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  997. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  998. * is truely gone. */
  999. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1000. if (ret == 0)
  1001. ret = -EAGAIN;
  1002. break;
  1003. }
  1004. if (i915_seqno_passed(ring->get_seqno(ring, false), seqno)) {
  1005. ret = 0;
  1006. break;
  1007. }
  1008. if (interruptible && signal_pending(current)) {
  1009. ret = -ERESTARTSYS;
  1010. break;
  1011. }
  1012. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1013. ret = -ETIME;
  1014. break;
  1015. }
  1016. timer.function = NULL;
  1017. if (timeout || missed_irq(dev_priv, ring)) {
  1018. unsigned long expire;
  1019. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1020. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1021. mod_timer(&timer, expire);
  1022. }
  1023. io_schedule();
  1024. if (timer.function) {
  1025. del_singleshot_timer_sync(&timer);
  1026. destroy_timer_on_stack(&timer);
  1027. }
  1028. }
  1029. getrawmonotonic(&now);
  1030. trace_i915_gem_request_wait_end(ring, seqno);
  1031. if (!irq_test_in_progress)
  1032. ring->irq_put(ring);
  1033. finish_wait(&ring->irq_queue, &wait);
  1034. if (timeout) {
  1035. struct timespec sleep_time = timespec_sub(now, before);
  1036. *timeout = timespec_sub(*timeout, sleep_time);
  1037. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  1038. set_normalized_timespec(timeout, 0, 0);
  1039. }
  1040. return ret;
  1041. }
  1042. /**
  1043. * Waits for a sequence number to be signaled, and cleans up the
  1044. * request and object lists appropriately for that event.
  1045. */
  1046. int
  1047. i915_wait_seqno(struct intel_engine_cs *ring, uint32_t seqno)
  1048. {
  1049. struct drm_device *dev = ring->dev;
  1050. struct drm_i915_private *dev_priv = dev->dev_private;
  1051. bool interruptible = dev_priv->mm.interruptible;
  1052. int ret;
  1053. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1054. BUG_ON(seqno == 0);
  1055. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1056. if (ret)
  1057. return ret;
  1058. ret = i915_gem_check_olr(ring, seqno);
  1059. if (ret)
  1060. return ret;
  1061. return __wait_seqno(ring, seqno,
  1062. atomic_read(&dev_priv->gpu_error.reset_counter),
  1063. interruptible, NULL, NULL);
  1064. }
  1065. static int
  1066. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  1067. struct intel_engine_cs *ring)
  1068. {
  1069. if (!obj->active)
  1070. return 0;
  1071. /* Manually manage the write flush as we may have not yet
  1072. * retired the buffer.
  1073. *
  1074. * Note that the last_write_seqno is always the earlier of
  1075. * the two (read/write) seqno, so if we haved successfully waited,
  1076. * we know we have passed the last write.
  1077. */
  1078. obj->last_write_seqno = 0;
  1079. return 0;
  1080. }
  1081. /**
  1082. * Ensures that all rendering to the object has completed and the object is
  1083. * safe to unbind from the GTT or access from the CPU.
  1084. */
  1085. static __must_check int
  1086. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1087. bool readonly)
  1088. {
  1089. struct intel_engine_cs *ring = obj->ring;
  1090. u32 seqno;
  1091. int ret;
  1092. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1093. if (seqno == 0)
  1094. return 0;
  1095. ret = i915_wait_seqno(ring, seqno);
  1096. if (ret)
  1097. return ret;
  1098. return i915_gem_object_wait_rendering__tail(obj, ring);
  1099. }
  1100. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1101. * as the object state may change during this call.
  1102. */
  1103. static __must_check int
  1104. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1105. struct drm_i915_file_private *file_priv,
  1106. bool readonly)
  1107. {
  1108. struct drm_device *dev = obj->base.dev;
  1109. struct drm_i915_private *dev_priv = dev->dev_private;
  1110. struct intel_engine_cs *ring = obj->ring;
  1111. unsigned reset_counter;
  1112. u32 seqno;
  1113. int ret;
  1114. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1115. BUG_ON(!dev_priv->mm.interruptible);
  1116. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1117. if (seqno == 0)
  1118. return 0;
  1119. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1120. if (ret)
  1121. return ret;
  1122. ret = i915_gem_check_olr(ring, seqno);
  1123. if (ret)
  1124. return ret;
  1125. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1126. mutex_unlock(&dev->struct_mutex);
  1127. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, file_priv);
  1128. mutex_lock(&dev->struct_mutex);
  1129. if (ret)
  1130. return ret;
  1131. return i915_gem_object_wait_rendering__tail(obj, ring);
  1132. }
  1133. /**
  1134. * Called when user space prepares to use an object with the CPU, either
  1135. * through the mmap ioctl's mapping or a GTT mapping.
  1136. */
  1137. int
  1138. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1139. struct drm_file *file)
  1140. {
  1141. struct drm_i915_gem_set_domain *args = data;
  1142. struct drm_i915_gem_object *obj;
  1143. uint32_t read_domains = args->read_domains;
  1144. uint32_t write_domain = args->write_domain;
  1145. int ret;
  1146. /* Only handle setting domains to types used by the CPU. */
  1147. if (write_domain & I915_GEM_GPU_DOMAINS)
  1148. return -EINVAL;
  1149. if (read_domains & I915_GEM_GPU_DOMAINS)
  1150. return -EINVAL;
  1151. /* Having something in the write domain implies it's in the read
  1152. * domain, and only that read domain. Enforce that in the request.
  1153. */
  1154. if (write_domain != 0 && read_domains != write_domain)
  1155. return -EINVAL;
  1156. ret = i915_mutex_lock_interruptible(dev);
  1157. if (ret)
  1158. return ret;
  1159. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1160. if (&obj->base == NULL) {
  1161. ret = -ENOENT;
  1162. goto unlock;
  1163. }
  1164. /* Try to flush the object off the GPU without holding the lock.
  1165. * We will repeat the flush holding the lock in the normal manner
  1166. * to catch cases where we are gazumped.
  1167. */
  1168. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1169. file->driver_priv,
  1170. !write_domain);
  1171. if (ret)
  1172. goto unref;
  1173. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1174. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1175. /* Silently promote "you're not bound, there was nothing to do"
  1176. * to success, since the client was just asking us to
  1177. * make sure everything was done.
  1178. */
  1179. if (ret == -EINVAL)
  1180. ret = 0;
  1181. } else {
  1182. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1183. }
  1184. unref:
  1185. drm_gem_object_unreference(&obj->base);
  1186. unlock:
  1187. mutex_unlock(&dev->struct_mutex);
  1188. return ret;
  1189. }
  1190. /**
  1191. * Called when user space has done writes to this buffer
  1192. */
  1193. int
  1194. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1195. struct drm_file *file)
  1196. {
  1197. struct drm_i915_gem_sw_finish *args = data;
  1198. struct drm_i915_gem_object *obj;
  1199. int ret = 0;
  1200. ret = i915_mutex_lock_interruptible(dev);
  1201. if (ret)
  1202. return ret;
  1203. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1204. if (&obj->base == NULL) {
  1205. ret = -ENOENT;
  1206. goto unlock;
  1207. }
  1208. /* Pinned buffers may be scanout, so flush the cache */
  1209. if (obj->pin_display)
  1210. i915_gem_object_flush_cpu_write_domain(obj, true);
  1211. drm_gem_object_unreference(&obj->base);
  1212. unlock:
  1213. mutex_unlock(&dev->struct_mutex);
  1214. return ret;
  1215. }
  1216. /**
  1217. * Maps the contents of an object, returning the address it is mapped
  1218. * into.
  1219. *
  1220. * While the mapping holds a reference on the contents of the object, it doesn't
  1221. * imply a ref on the object itself.
  1222. */
  1223. int
  1224. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1225. struct drm_file *file)
  1226. {
  1227. struct drm_i915_gem_mmap *args = data;
  1228. struct drm_gem_object *obj;
  1229. unsigned long addr;
  1230. obj = drm_gem_object_lookup(dev, file, args->handle);
  1231. if (obj == NULL)
  1232. return -ENOENT;
  1233. /* prime objects have no backing filp to GEM mmap
  1234. * pages from.
  1235. */
  1236. if (!obj->filp) {
  1237. drm_gem_object_unreference_unlocked(obj);
  1238. return -EINVAL;
  1239. }
  1240. addr = vm_mmap(obj->filp, 0, args->size,
  1241. PROT_READ | PROT_WRITE, MAP_SHARED,
  1242. args->offset);
  1243. drm_gem_object_unreference_unlocked(obj);
  1244. if (IS_ERR((void *)addr))
  1245. return addr;
  1246. args->addr_ptr = (uint64_t) addr;
  1247. return 0;
  1248. }
  1249. /**
  1250. * i915_gem_fault - fault a page into the GTT
  1251. * vma: VMA in question
  1252. * vmf: fault info
  1253. *
  1254. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1255. * from userspace. The fault handler takes care of binding the object to
  1256. * the GTT (if needed), allocating and programming a fence register (again,
  1257. * only if needed based on whether the old reg is still valid or the object
  1258. * is tiled) and inserting a new PTE into the faulting process.
  1259. *
  1260. * Note that the faulting process may involve evicting existing objects
  1261. * from the GTT and/or fence registers to make room. So performance may
  1262. * suffer if the GTT working set is large or there are few fence registers
  1263. * left.
  1264. */
  1265. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1266. {
  1267. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1268. struct drm_device *dev = obj->base.dev;
  1269. struct drm_i915_private *dev_priv = dev->dev_private;
  1270. pgoff_t page_offset;
  1271. unsigned long pfn;
  1272. int ret = 0;
  1273. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1274. intel_runtime_pm_get(dev_priv);
  1275. /* We don't use vmf->pgoff since that has the fake offset */
  1276. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1277. PAGE_SHIFT;
  1278. ret = i915_mutex_lock_interruptible(dev);
  1279. if (ret)
  1280. goto out;
  1281. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1282. /* Try to flush the object off the GPU first without holding the lock.
  1283. * Upon reacquiring the lock, we will perform our sanity checks and then
  1284. * repeat the flush holding the lock in the normal manner to catch cases
  1285. * where we are gazumped.
  1286. */
  1287. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1288. if (ret)
  1289. goto unlock;
  1290. /* Access to snoopable pages through the GTT is incoherent. */
  1291. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1292. ret = -EFAULT;
  1293. goto unlock;
  1294. }
  1295. /* Now bind it into the GTT if needed */
  1296. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
  1297. if (ret)
  1298. goto unlock;
  1299. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1300. if (ret)
  1301. goto unpin;
  1302. ret = i915_gem_object_get_fence(obj);
  1303. if (ret)
  1304. goto unpin;
  1305. /* Finally, remap it using the new GTT offset */
  1306. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1307. pfn >>= PAGE_SHIFT;
  1308. if (!obj->fault_mappable) {
  1309. unsigned long size = min_t(unsigned long,
  1310. vma->vm_end - vma->vm_start,
  1311. obj->base.size);
  1312. int i;
  1313. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1314. ret = vm_insert_pfn(vma,
  1315. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1316. pfn + i);
  1317. if (ret)
  1318. break;
  1319. }
  1320. obj->fault_mappable = true;
  1321. } else
  1322. ret = vm_insert_pfn(vma,
  1323. (unsigned long)vmf->virtual_address,
  1324. pfn + page_offset);
  1325. unpin:
  1326. i915_gem_object_ggtt_unpin(obj);
  1327. unlock:
  1328. mutex_unlock(&dev->struct_mutex);
  1329. out:
  1330. switch (ret) {
  1331. case -EIO:
  1332. /* If this -EIO is due to a gpu hang, give the reset code a
  1333. * chance to clean up the mess. Otherwise return the proper
  1334. * SIGBUS. */
  1335. if (i915_terminally_wedged(&dev_priv->gpu_error)) {
  1336. ret = VM_FAULT_SIGBUS;
  1337. break;
  1338. }
  1339. case -EAGAIN:
  1340. /*
  1341. * EAGAIN means the gpu is hung and we'll wait for the error
  1342. * handler to reset everything when re-faulting in
  1343. * i915_mutex_lock_interruptible.
  1344. */
  1345. case 0:
  1346. case -ERESTARTSYS:
  1347. case -EINTR:
  1348. case -EBUSY:
  1349. /*
  1350. * EBUSY is ok: this just means that another thread
  1351. * already did the job.
  1352. */
  1353. ret = VM_FAULT_NOPAGE;
  1354. break;
  1355. case -ENOMEM:
  1356. ret = VM_FAULT_OOM;
  1357. break;
  1358. case -ENOSPC:
  1359. case -EFAULT:
  1360. ret = VM_FAULT_SIGBUS;
  1361. break;
  1362. default:
  1363. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1364. ret = VM_FAULT_SIGBUS;
  1365. break;
  1366. }
  1367. intel_runtime_pm_put(dev_priv);
  1368. return ret;
  1369. }
  1370. /**
  1371. * i915_gem_release_mmap - remove physical page mappings
  1372. * @obj: obj in question
  1373. *
  1374. * Preserve the reservation of the mmapping with the DRM core code, but
  1375. * relinquish ownership of the pages back to the system.
  1376. *
  1377. * It is vital that we remove the page mapping if we have mapped a tiled
  1378. * object through the GTT and then lose the fence register due to
  1379. * resource pressure. Similarly if the object has been moved out of the
  1380. * aperture, than pages mapped into userspace must be revoked. Removing the
  1381. * mapping will then trigger a page fault on the next user access, allowing
  1382. * fixup by i915_gem_fault().
  1383. */
  1384. void
  1385. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1386. {
  1387. if (!obj->fault_mappable)
  1388. return;
  1389. drm_vma_node_unmap(&obj->base.vma_node,
  1390. obj->base.dev->anon_inode->i_mapping);
  1391. obj->fault_mappable = false;
  1392. }
  1393. void
  1394. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1395. {
  1396. struct drm_i915_gem_object *obj;
  1397. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1398. i915_gem_release_mmap(obj);
  1399. }
  1400. uint32_t
  1401. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1402. {
  1403. uint32_t gtt_size;
  1404. if (INTEL_INFO(dev)->gen >= 4 ||
  1405. tiling_mode == I915_TILING_NONE)
  1406. return size;
  1407. /* Previous chips need a power-of-two fence region when tiling */
  1408. if (INTEL_INFO(dev)->gen == 3)
  1409. gtt_size = 1024*1024;
  1410. else
  1411. gtt_size = 512*1024;
  1412. while (gtt_size < size)
  1413. gtt_size <<= 1;
  1414. return gtt_size;
  1415. }
  1416. /**
  1417. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1418. * @obj: object to check
  1419. *
  1420. * Return the required GTT alignment for an object, taking into account
  1421. * potential fence register mapping.
  1422. */
  1423. uint32_t
  1424. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1425. int tiling_mode, bool fenced)
  1426. {
  1427. /*
  1428. * Minimum alignment is 4k (GTT page size), but might be greater
  1429. * if a fence register is needed for the object.
  1430. */
  1431. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1432. tiling_mode == I915_TILING_NONE)
  1433. return 4096;
  1434. /*
  1435. * Previous chips need to be aligned to the size of the smallest
  1436. * fence register that can contain the object.
  1437. */
  1438. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1439. }
  1440. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1441. {
  1442. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1443. int ret;
  1444. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1445. return 0;
  1446. dev_priv->mm.shrinker_no_lock_stealing = true;
  1447. ret = drm_gem_create_mmap_offset(&obj->base);
  1448. if (ret != -ENOSPC)
  1449. goto out;
  1450. /* Badly fragmented mmap space? The only way we can recover
  1451. * space is by destroying unwanted objects. We can't randomly release
  1452. * mmap_offsets as userspace expects them to be persistent for the
  1453. * lifetime of the objects. The closest we can is to release the
  1454. * offsets on purgeable objects by truncating it and marking it purged,
  1455. * which prevents userspace from ever using that object again.
  1456. */
  1457. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1458. ret = drm_gem_create_mmap_offset(&obj->base);
  1459. if (ret != -ENOSPC)
  1460. goto out;
  1461. i915_gem_shrink_all(dev_priv);
  1462. ret = drm_gem_create_mmap_offset(&obj->base);
  1463. out:
  1464. dev_priv->mm.shrinker_no_lock_stealing = false;
  1465. return ret;
  1466. }
  1467. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1468. {
  1469. drm_gem_free_mmap_offset(&obj->base);
  1470. }
  1471. int
  1472. i915_gem_mmap_gtt(struct drm_file *file,
  1473. struct drm_device *dev,
  1474. uint32_t handle,
  1475. uint64_t *offset)
  1476. {
  1477. struct drm_i915_private *dev_priv = dev->dev_private;
  1478. struct drm_i915_gem_object *obj;
  1479. int ret;
  1480. ret = i915_mutex_lock_interruptible(dev);
  1481. if (ret)
  1482. return ret;
  1483. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1484. if (&obj->base == NULL) {
  1485. ret = -ENOENT;
  1486. goto unlock;
  1487. }
  1488. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1489. ret = -E2BIG;
  1490. goto out;
  1491. }
  1492. if (obj->madv != I915_MADV_WILLNEED) {
  1493. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1494. ret = -EFAULT;
  1495. goto out;
  1496. }
  1497. ret = i915_gem_object_create_mmap_offset(obj);
  1498. if (ret)
  1499. goto out;
  1500. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1501. out:
  1502. drm_gem_object_unreference(&obj->base);
  1503. unlock:
  1504. mutex_unlock(&dev->struct_mutex);
  1505. return ret;
  1506. }
  1507. /**
  1508. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1509. * @dev: DRM device
  1510. * @data: GTT mapping ioctl data
  1511. * @file: GEM object info
  1512. *
  1513. * Simply returns the fake offset to userspace so it can mmap it.
  1514. * The mmap call will end up in drm_gem_mmap(), which will set things
  1515. * up so we can get faults in the handler above.
  1516. *
  1517. * The fault handler will take care of binding the object into the GTT
  1518. * (since it may have been evicted to make room for something), allocating
  1519. * a fence register, and mapping the appropriate aperture address into
  1520. * userspace.
  1521. */
  1522. int
  1523. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1524. struct drm_file *file)
  1525. {
  1526. struct drm_i915_gem_mmap_gtt *args = data;
  1527. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1528. }
  1529. static inline int
  1530. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1531. {
  1532. return obj->madv == I915_MADV_DONTNEED;
  1533. }
  1534. /* Immediately discard the backing storage */
  1535. static void
  1536. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1537. {
  1538. i915_gem_object_free_mmap_offset(obj);
  1539. if (obj->base.filp == NULL)
  1540. return;
  1541. /* Our goal here is to return as much of the memory as
  1542. * is possible back to the system as we are called from OOM.
  1543. * To do this we must instruct the shmfs to drop all of its
  1544. * backing pages, *now*.
  1545. */
  1546. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1547. obj->madv = __I915_MADV_PURGED;
  1548. }
  1549. /* Try to discard unwanted pages */
  1550. static void
  1551. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1552. {
  1553. struct address_space *mapping;
  1554. switch (obj->madv) {
  1555. case I915_MADV_DONTNEED:
  1556. i915_gem_object_truncate(obj);
  1557. case __I915_MADV_PURGED:
  1558. return;
  1559. }
  1560. if (obj->base.filp == NULL)
  1561. return;
  1562. mapping = file_inode(obj->base.filp)->i_mapping,
  1563. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1564. }
  1565. static void
  1566. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1567. {
  1568. struct sg_page_iter sg_iter;
  1569. int ret;
  1570. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1571. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1572. if (ret) {
  1573. /* In the event of a disaster, abandon all caches and
  1574. * hope for the best.
  1575. */
  1576. WARN_ON(ret != -EIO);
  1577. i915_gem_clflush_object(obj, true);
  1578. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1579. }
  1580. if (i915_gem_object_needs_bit17_swizzle(obj))
  1581. i915_gem_object_save_bit_17_swizzle(obj);
  1582. if (obj->madv == I915_MADV_DONTNEED)
  1583. obj->dirty = 0;
  1584. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1585. struct page *page = sg_page_iter_page(&sg_iter);
  1586. if (obj->dirty)
  1587. set_page_dirty(page);
  1588. if (obj->madv == I915_MADV_WILLNEED)
  1589. mark_page_accessed(page);
  1590. page_cache_release(page);
  1591. }
  1592. obj->dirty = 0;
  1593. sg_free_table(obj->pages);
  1594. kfree(obj->pages);
  1595. }
  1596. int
  1597. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1598. {
  1599. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1600. if (obj->pages == NULL)
  1601. return 0;
  1602. if (obj->pages_pin_count)
  1603. return -EBUSY;
  1604. BUG_ON(i915_gem_obj_bound_any(obj));
  1605. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1606. * array, hence protect them from being reaped by removing them from gtt
  1607. * lists early. */
  1608. list_del(&obj->global_list);
  1609. ops->put_pages(obj);
  1610. obj->pages = NULL;
  1611. i915_gem_object_invalidate(obj);
  1612. return 0;
  1613. }
  1614. static unsigned long
  1615. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1616. bool purgeable_only)
  1617. {
  1618. struct list_head still_in_list;
  1619. struct drm_i915_gem_object *obj;
  1620. unsigned long count = 0;
  1621. /*
  1622. * As we may completely rewrite the (un)bound list whilst unbinding
  1623. * (due to retiring requests) we have to strictly process only
  1624. * one element of the list at the time, and recheck the list
  1625. * on every iteration.
  1626. *
  1627. * In particular, we must hold a reference whilst removing the
  1628. * object as we may end up waiting for and/or retiring the objects.
  1629. * This might release the final reference (held by the active list)
  1630. * and result in the object being freed from under us. This is
  1631. * similar to the precautions the eviction code must take whilst
  1632. * removing objects.
  1633. *
  1634. * Also note that although these lists do not hold a reference to
  1635. * the object we can safely grab one here: The final object
  1636. * unreferencing and the bound_list are both protected by the
  1637. * dev->struct_mutex and so we won't ever be able to observe an
  1638. * object on the bound_list with a reference count equals 0.
  1639. */
  1640. INIT_LIST_HEAD(&still_in_list);
  1641. while (count < target && !list_empty(&dev_priv->mm.unbound_list)) {
  1642. obj = list_first_entry(&dev_priv->mm.unbound_list,
  1643. typeof(*obj), global_list);
  1644. list_move_tail(&obj->global_list, &still_in_list);
  1645. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1646. continue;
  1647. drm_gem_object_reference(&obj->base);
  1648. if (i915_gem_object_put_pages(obj) == 0)
  1649. count += obj->base.size >> PAGE_SHIFT;
  1650. drm_gem_object_unreference(&obj->base);
  1651. }
  1652. list_splice(&still_in_list, &dev_priv->mm.unbound_list);
  1653. INIT_LIST_HEAD(&still_in_list);
  1654. while (count < target && !list_empty(&dev_priv->mm.bound_list)) {
  1655. struct i915_vma *vma, *v;
  1656. obj = list_first_entry(&dev_priv->mm.bound_list,
  1657. typeof(*obj), global_list);
  1658. list_move_tail(&obj->global_list, &still_in_list);
  1659. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1660. continue;
  1661. drm_gem_object_reference(&obj->base);
  1662. list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
  1663. if (i915_vma_unbind(vma))
  1664. break;
  1665. if (i915_gem_object_put_pages(obj) == 0)
  1666. count += obj->base.size >> PAGE_SHIFT;
  1667. drm_gem_object_unreference(&obj->base);
  1668. }
  1669. list_splice(&still_in_list, &dev_priv->mm.bound_list);
  1670. return count;
  1671. }
  1672. static unsigned long
  1673. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1674. {
  1675. return __i915_gem_shrink(dev_priv, target, true);
  1676. }
  1677. static unsigned long
  1678. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1679. {
  1680. i915_gem_evict_everything(dev_priv->dev);
  1681. return __i915_gem_shrink(dev_priv, LONG_MAX, false);
  1682. }
  1683. static int
  1684. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1685. {
  1686. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1687. int page_count, i;
  1688. struct address_space *mapping;
  1689. struct sg_table *st;
  1690. struct scatterlist *sg;
  1691. struct sg_page_iter sg_iter;
  1692. struct page *page;
  1693. unsigned long last_pfn = 0; /* suppress gcc warning */
  1694. gfp_t gfp;
  1695. /* Assert that the object is not currently in any GPU domain. As it
  1696. * wasn't in the GTT, there shouldn't be any way it could have been in
  1697. * a GPU cache
  1698. */
  1699. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1700. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1701. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1702. if (st == NULL)
  1703. return -ENOMEM;
  1704. page_count = obj->base.size / PAGE_SIZE;
  1705. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1706. kfree(st);
  1707. return -ENOMEM;
  1708. }
  1709. /* Get the list of pages out of our struct file. They'll be pinned
  1710. * at this point until we release them.
  1711. *
  1712. * Fail silently without starting the shrinker
  1713. */
  1714. mapping = file_inode(obj->base.filp)->i_mapping;
  1715. gfp = mapping_gfp_mask(mapping);
  1716. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1717. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1718. sg = st->sgl;
  1719. st->nents = 0;
  1720. for (i = 0; i < page_count; i++) {
  1721. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1722. if (IS_ERR(page)) {
  1723. i915_gem_purge(dev_priv, page_count);
  1724. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1725. }
  1726. if (IS_ERR(page)) {
  1727. /* We've tried hard to allocate the memory by reaping
  1728. * our own buffer, now let the real VM do its job and
  1729. * go down in flames if truly OOM.
  1730. */
  1731. i915_gem_shrink_all(dev_priv);
  1732. page = shmem_read_mapping_page(mapping, i);
  1733. if (IS_ERR(page))
  1734. goto err_pages;
  1735. }
  1736. #ifdef CONFIG_SWIOTLB
  1737. if (swiotlb_nr_tbl()) {
  1738. st->nents++;
  1739. sg_set_page(sg, page, PAGE_SIZE, 0);
  1740. sg = sg_next(sg);
  1741. continue;
  1742. }
  1743. #endif
  1744. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1745. if (i)
  1746. sg = sg_next(sg);
  1747. st->nents++;
  1748. sg_set_page(sg, page, PAGE_SIZE, 0);
  1749. } else {
  1750. sg->length += PAGE_SIZE;
  1751. }
  1752. last_pfn = page_to_pfn(page);
  1753. /* Check that the i965g/gm workaround works. */
  1754. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1755. }
  1756. #ifdef CONFIG_SWIOTLB
  1757. if (!swiotlb_nr_tbl())
  1758. #endif
  1759. sg_mark_end(sg);
  1760. obj->pages = st;
  1761. if (i915_gem_object_needs_bit17_swizzle(obj))
  1762. i915_gem_object_do_bit_17_swizzle(obj);
  1763. return 0;
  1764. err_pages:
  1765. sg_mark_end(sg);
  1766. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1767. page_cache_release(sg_page_iter_page(&sg_iter));
  1768. sg_free_table(st);
  1769. kfree(st);
  1770. /* shmemfs first checks if there is enough memory to allocate the page
  1771. * and reports ENOSPC should there be insufficient, along with the usual
  1772. * ENOMEM for a genuine allocation failure.
  1773. *
  1774. * We use ENOSPC in our driver to mean that we have run out of aperture
  1775. * space and so want to translate the error from shmemfs back to our
  1776. * usual understanding of ENOMEM.
  1777. */
  1778. if (PTR_ERR(page) == -ENOSPC)
  1779. return -ENOMEM;
  1780. else
  1781. return PTR_ERR(page);
  1782. }
  1783. /* Ensure that the associated pages are gathered from the backing storage
  1784. * and pinned into our object. i915_gem_object_get_pages() may be called
  1785. * multiple times before they are released by a single call to
  1786. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1787. * either as a result of memory pressure (reaping pages under the shrinker)
  1788. * or as the object is itself released.
  1789. */
  1790. int
  1791. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1792. {
  1793. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1794. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1795. int ret;
  1796. if (obj->pages)
  1797. return 0;
  1798. if (obj->madv != I915_MADV_WILLNEED) {
  1799. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1800. return -EFAULT;
  1801. }
  1802. BUG_ON(obj->pages_pin_count);
  1803. ret = ops->get_pages(obj);
  1804. if (ret)
  1805. return ret;
  1806. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1807. return 0;
  1808. }
  1809. static void
  1810. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1811. struct intel_engine_cs *ring)
  1812. {
  1813. u32 seqno = intel_ring_get_seqno(ring);
  1814. BUG_ON(ring == NULL);
  1815. if (obj->ring != ring && obj->last_write_seqno) {
  1816. /* Keep the seqno relative to the current ring */
  1817. obj->last_write_seqno = seqno;
  1818. }
  1819. obj->ring = ring;
  1820. /* Add a reference if we're newly entering the active list. */
  1821. if (!obj->active) {
  1822. drm_gem_object_reference(&obj->base);
  1823. obj->active = 1;
  1824. }
  1825. list_move_tail(&obj->ring_list, &ring->active_list);
  1826. obj->last_read_seqno = seqno;
  1827. }
  1828. void i915_vma_move_to_active(struct i915_vma *vma,
  1829. struct intel_engine_cs *ring)
  1830. {
  1831. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1832. return i915_gem_object_move_to_active(vma->obj, ring);
  1833. }
  1834. static void
  1835. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1836. {
  1837. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1838. struct i915_address_space *vm;
  1839. struct i915_vma *vma;
  1840. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1841. BUG_ON(!obj->active);
  1842. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1843. vma = i915_gem_obj_to_vma(obj, vm);
  1844. if (vma && !list_empty(&vma->mm_list))
  1845. list_move_tail(&vma->mm_list, &vm->inactive_list);
  1846. }
  1847. intel_fb_obj_flush(obj, true);
  1848. list_del_init(&obj->ring_list);
  1849. obj->ring = NULL;
  1850. obj->last_read_seqno = 0;
  1851. obj->last_write_seqno = 0;
  1852. obj->base.write_domain = 0;
  1853. obj->last_fenced_seqno = 0;
  1854. obj->active = 0;
  1855. drm_gem_object_unreference(&obj->base);
  1856. WARN_ON(i915_verify_lists(dev));
  1857. }
  1858. static void
  1859. i915_gem_object_retire(struct drm_i915_gem_object *obj)
  1860. {
  1861. struct intel_engine_cs *ring = obj->ring;
  1862. if (ring == NULL)
  1863. return;
  1864. if (i915_seqno_passed(ring->get_seqno(ring, true),
  1865. obj->last_read_seqno))
  1866. i915_gem_object_move_to_inactive(obj);
  1867. }
  1868. static int
  1869. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1870. {
  1871. struct drm_i915_private *dev_priv = dev->dev_private;
  1872. struct intel_engine_cs *ring;
  1873. int ret, i, j;
  1874. /* Carefully retire all requests without writing to the rings */
  1875. for_each_ring(ring, dev_priv, i) {
  1876. ret = intel_ring_idle(ring);
  1877. if (ret)
  1878. return ret;
  1879. }
  1880. i915_gem_retire_requests(dev);
  1881. /* Finally reset hw state */
  1882. for_each_ring(ring, dev_priv, i) {
  1883. intel_ring_init_seqno(ring, seqno);
  1884. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  1885. ring->semaphore.sync_seqno[j] = 0;
  1886. }
  1887. return 0;
  1888. }
  1889. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1890. {
  1891. struct drm_i915_private *dev_priv = dev->dev_private;
  1892. int ret;
  1893. if (seqno == 0)
  1894. return -EINVAL;
  1895. /* HWS page needs to be set less than what we
  1896. * will inject to ring
  1897. */
  1898. ret = i915_gem_init_seqno(dev, seqno - 1);
  1899. if (ret)
  1900. return ret;
  1901. /* Carefully set the last_seqno value so that wrap
  1902. * detection still works
  1903. */
  1904. dev_priv->next_seqno = seqno;
  1905. dev_priv->last_seqno = seqno - 1;
  1906. if (dev_priv->last_seqno == 0)
  1907. dev_priv->last_seqno--;
  1908. return 0;
  1909. }
  1910. int
  1911. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1912. {
  1913. struct drm_i915_private *dev_priv = dev->dev_private;
  1914. /* reserve 0 for non-seqno */
  1915. if (dev_priv->next_seqno == 0) {
  1916. int ret = i915_gem_init_seqno(dev, 0);
  1917. if (ret)
  1918. return ret;
  1919. dev_priv->next_seqno = 1;
  1920. }
  1921. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1922. return 0;
  1923. }
  1924. int __i915_add_request(struct intel_engine_cs *ring,
  1925. struct drm_file *file,
  1926. struct drm_i915_gem_object *obj,
  1927. u32 *out_seqno)
  1928. {
  1929. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1930. struct drm_i915_gem_request *request;
  1931. struct intel_ringbuffer *ringbuf;
  1932. u32 request_ring_position, request_start;
  1933. int ret;
  1934. request = ring->preallocated_lazy_request;
  1935. if (WARN_ON(request == NULL))
  1936. return -ENOMEM;
  1937. if (i915.enable_execlists) {
  1938. struct intel_context *ctx = request->ctx;
  1939. ringbuf = ctx->engine[ring->id].ringbuf;
  1940. } else
  1941. ringbuf = ring->buffer;
  1942. request_start = intel_ring_get_tail(ringbuf);
  1943. /*
  1944. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1945. * after having emitted the batchbuffer command. Hence we need to fix
  1946. * things up similar to emitting the lazy request. The difference here
  1947. * is that the flush _must_ happen before the next request, no matter
  1948. * what.
  1949. */
  1950. if (i915.enable_execlists) {
  1951. ret = logical_ring_flush_all_caches(ringbuf);
  1952. if (ret)
  1953. return ret;
  1954. } else {
  1955. ret = intel_ring_flush_all_caches(ring);
  1956. if (ret)
  1957. return ret;
  1958. }
  1959. /* Record the position of the start of the request so that
  1960. * should we detect the updated seqno part-way through the
  1961. * GPU processing the request, we never over-estimate the
  1962. * position of the head.
  1963. */
  1964. request_ring_position = intel_ring_get_tail(ringbuf);
  1965. if (i915.enable_execlists) {
  1966. ret = ring->emit_request(ringbuf);
  1967. if (ret)
  1968. return ret;
  1969. } else {
  1970. ret = ring->add_request(ring);
  1971. if (ret)
  1972. return ret;
  1973. }
  1974. request->seqno = intel_ring_get_seqno(ring);
  1975. request->ring = ring;
  1976. request->head = request_start;
  1977. request->tail = request_ring_position;
  1978. /* Whilst this request exists, batch_obj will be on the
  1979. * active_list, and so will hold the active reference. Only when this
  1980. * request is retired will the the batch_obj be moved onto the
  1981. * inactive_list and lose its active reference. Hence we do not need
  1982. * to explicitly hold another reference here.
  1983. */
  1984. request->batch_obj = obj;
  1985. if (!i915.enable_execlists) {
  1986. /* Hold a reference to the current context so that we can inspect
  1987. * it later in case a hangcheck error event fires.
  1988. */
  1989. request->ctx = ring->last_context;
  1990. if (request->ctx)
  1991. i915_gem_context_reference(request->ctx);
  1992. }
  1993. request->emitted_jiffies = jiffies;
  1994. list_add_tail(&request->list, &ring->request_list);
  1995. request->file_priv = NULL;
  1996. if (file) {
  1997. struct drm_i915_file_private *file_priv = file->driver_priv;
  1998. spin_lock(&file_priv->mm.lock);
  1999. request->file_priv = file_priv;
  2000. list_add_tail(&request->client_list,
  2001. &file_priv->mm.request_list);
  2002. spin_unlock(&file_priv->mm.lock);
  2003. }
  2004. trace_i915_gem_request_add(ring, request->seqno);
  2005. ring->outstanding_lazy_seqno = 0;
  2006. ring->preallocated_lazy_request = NULL;
  2007. if (!dev_priv->ums.mm_suspended) {
  2008. i915_queue_hangcheck(ring->dev);
  2009. cancel_delayed_work_sync(&dev_priv->mm.idle_work);
  2010. queue_delayed_work(dev_priv->wq,
  2011. &dev_priv->mm.retire_work,
  2012. round_jiffies_up_relative(HZ));
  2013. intel_mark_busy(dev_priv->dev);
  2014. }
  2015. if (out_seqno)
  2016. *out_seqno = request->seqno;
  2017. return 0;
  2018. }
  2019. static inline void
  2020. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  2021. {
  2022. struct drm_i915_file_private *file_priv = request->file_priv;
  2023. if (!file_priv)
  2024. return;
  2025. spin_lock(&file_priv->mm.lock);
  2026. list_del(&request->client_list);
  2027. request->file_priv = NULL;
  2028. spin_unlock(&file_priv->mm.lock);
  2029. }
  2030. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2031. const struct intel_context *ctx)
  2032. {
  2033. unsigned long elapsed;
  2034. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2035. if (ctx->hang_stats.banned)
  2036. return true;
  2037. if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
  2038. if (!i915_gem_context_is_default(ctx)) {
  2039. DRM_DEBUG("context hanging too fast, banning!\n");
  2040. return true;
  2041. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2042. if (i915_stop_ring_allow_warn(dev_priv))
  2043. DRM_ERROR("gpu hanging too fast, banning!\n");
  2044. return true;
  2045. }
  2046. }
  2047. return false;
  2048. }
  2049. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2050. struct intel_context *ctx,
  2051. const bool guilty)
  2052. {
  2053. struct i915_ctx_hang_stats *hs;
  2054. if (WARN_ON(!ctx))
  2055. return;
  2056. hs = &ctx->hang_stats;
  2057. if (guilty) {
  2058. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2059. hs->batch_active++;
  2060. hs->guilty_ts = get_seconds();
  2061. } else {
  2062. hs->batch_pending++;
  2063. }
  2064. }
  2065. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  2066. {
  2067. list_del(&request->list);
  2068. i915_gem_request_remove_from_client(request);
  2069. if (request->ctx)
  2070. i915_gem_context_unreference(request->ctx);
  2071. kfree(request);
  2072. }
  2073. struct drm_i915_gem_request *
  2074. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2075. {
  2076. struct drm_i915_gem_request *request;
  2077. u32 completed_seqno;
  2078. completed_seqno = ring->get_seqno(ring, false);
  2079. list_for_each_entry(request, &ring->request_list, list) {
  2080. if (i915_seqno_passed(completed_seqno, request->seqno))
  2081. continue;
  2082. return request;
  2083. }
  2084. return NULL;
  2085. }
  2086. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2087. struct intel_engine_cs *ring)
  2088. {
  2089. struct drm_i915_gem_request *request;
  2090. bool ring_hung;
  2091. request = i915_gem_find_active_request(ring);
  2092. if (request == NULL)
  2093. return;
  2094. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2095. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2096. list_for_each_entry_continue(request, &ring->request_list, list)
  2097. i915_set_reset_status(dev_priv, request->ctx, false);
  2098. }
  2099. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2100. struct intel_engine_cs *ring)
  2101. {
  2102. while (!list_empty(&ring->active_list)) {
  2103. struct drm_i915_gem_object *obj;
  2104. obj = list_first_entry(&ring->active_list,
  2105. struct drm_i915_gem_object,
  2106. ring_list);
  2107. i915_gem_object_move_to_inactive(obj);
  2108. }
  2109. /*
  2110. * We must free the requests after all the corresponding objects have
  2111. * been moved off active lists. Which is the same order as the normal
  2112. * retire_requests function does. This is important if object hold
  2113. * implicit references on things like e.g. ppgtt address spaces through
  2114. * the request.
  2115. */
  2116. while (!list_empty(&ring->request_list)) {
  2117. struct drm_i915_gem_request *request;
  2118. request = list_first_entry(&ring->request_list,
  2119. struct drm_i915_gem_request,
  2120. list);
  2121. i915_gem_free_request(request);
  2122. }
  2123. while (!list_empty(&ring->execlist_queue)) {
  2124. struct intel_ctx_submit_request *submit_req;
  2125. submit_req = list_first_entry(&ring->execlist_queue,
  2126. struct intel_ctx_submit_request,
  2127. execlist_link);
  2128. list_del(&submit_req->execlist_link);
  2129. intel_runtime_pm_put(dev_priv);
  2130. i915_gem_context_unreference(submit_req->ctx);
  2131. kfree(submit_req);
  2132. }
  2133. /* These may not have been flush before the reset, do so now */
  2134. kfree(ring->preallocated_lazy_request);
  2135. ring->preallocated_lazy_request = NULL;
  2136. ring->outstanding_lazy_seqno = 0;
  2137. }
  2138. void i915_gem_restore_fences(struct drm_device *dev)
  2139. {
  2140. struct drm_i915_private *dev_priv = dev->dev_private;
  2141. int i;
  2142. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  2143. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  2144. /*
  2145. * Commit delayed tiling changes if we have an object still
  2146. * attached to the fence, otherwise just clear the fence.
  2147. */
  2148. if (reg->obj) {
  2149. i915_gem_object_update_fence(reg->obj, reg,
  2150. reg->obj->tiling_mode);
  2151. } else {
  2152. i915_gem_write_fence(dev, i, NULL);
  2153. }
  2154. }
  2155. }
  2156. void i915_gem_reset(struct drm_device *dev)
  2157. {
  2158. struct drm_i915_private *dev_priv = dev->dev_private;
  2159. struct intel_engine_cs *ring;
  2160. int i;
  2161. /*
  2162. * Before we free the objects from the requests, we need to inspect
  2163. * them for finding the guilty party. As the requests only borrow
  2164. * their reference to the objects, the inspection must be done first.
  2165. */
  2166. for_each_ring(ring, dev_priv, i)
  2167. i915_gem_reset_ring_status(dev_priv, ring);
  2168. for_each_ring(ring, dev_priv, i)
  2169. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2170. i915_gem_context_reset(dev);
  2171. i915_gem_restore_fences(dev);
  2172. }
  2173. /**
  2174. * This function clears the request list as sequence numbers are passed.
  2175. */
  2176. void
  2177. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2178. {
  2179. uint32_t seqno;
  2180. if (list_empty(&ring->request_list))
  2181. return;
  2182. WARN_ON(i915_verify_lists(ring->dev));
  2183. seqno = ring->get_seqno(ring, true);
  2184. /* Move any buffers on the active list that are no longer referenced
  2185. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2186. * before we free the context associated with the requests.
  2187. */
  2188. while (!list_empty(&ring->active_list)) {
  2189. struct drm_i915_gem_object *obj;
  2190. obj = list_first_entry(&ring->active_list,
  2191. struct drm_i915_gem_object,
  2192. ring_list);
  2193. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  2194. break;
  2195. i915_gem_object_move_to_inactive(obj);
  2196. }
  2197. while (!list_empty(&ring->request_list)) {
  2198. struct drm_i915_gem_request *request;
  2199. struct intel_ringbuffer *ringbuf;
  2200. request = list_first_entry(&ring->request_list,
  2201. struct drm_i915_gem_request,
  2202. list);
  2203. if (!i915_seqno_passed(seqno, request->seqno))
  2204. break;
  2205. trace_i915_gem_request_retire(ring, request->seqno);
  2206. /* This is one of the few common intersection points
  2207. * between legacy ringbuffer submission and execlists:
  2208. * we need to tell them apart in order to find the correct
  2209. * ringbuffer to which the request belongs to.
  2210. */
  2211. if (i915.enable_execlists) {
  2212. struct intel_context *ctx = request->ctx;
  2213. ringbuf = ctx->engine[ring->id].ringbuf;
  2214. } else
  2215. ringbuf = ring->buffer;
  2216. /* We know the GPU must have read the request to have
  2217. * sent us the seqno + interrupt, so use the position
  2218. * of tail of the request to update the last known position
  2219. * of the GPU head.
  2220. */
  2221. ringbuf->last_retired_head = request->tail;
  2222. i915_gem_free_request(request);
  2223. }
  2224. if (unlikely(ring->trace_irq_seqno &&
  2225. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  2226. ring->irq_put(ring);
  2227. ring->trace_irq_seqno = 0;
  2228. }
  2229. WARN_ON(i915_verify_lists(ring->dev));
  2230. }
  2231. bool
  2232. i915_gem_retire_requests(struct drm_device *dev)
  2233. {
  2234. struct drm_i915_private *dev_priv = dev->dev_private;
  2235. struct intel_engine_cs *ring;
  2236. bool idle = true;
  2237. int i;
  2238. for_each_ring(ring, dev_priv, i) {
  2239. i915_gem_retire_requests_ring(ring);
  2240. idle &= list_empty(&ring->request_list);
  2241. }
  2242. if (idle)
  2243. mod_delayed_work(dev_priv->wq,
  2244. &dev_priv->mm.idle_work,
  2245. msecs_to_jiffies(100));
  2246. return idle;
  2247. }
  2248. static void
  2249. i915_gem_retire_work_handler(struct work_struct *work)
  2250. {
  2251. struct drm_i915_private *dev_priv =
  2252. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2253. struct drm_device *dev = dev_priv->dev;
  2254. bool idle;
  2255. /* Come back later if the device is busy... */
  2256. idle = false;
  2257. if (mutex_trylock(&dev->struct_mutex)) {
  2258. idle = i915_gem_retire_requests(dev);
  2259. mutex_unlock(&dev->struct_mutex);
  2260. }
  2261. if (!idle)
  2262. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2263. round_jiffies_up_relative(HZ));
  2264. }
  2265. static void
  2266. i915_gem_idle_work_handler(struct work_struct *work)
  2267. {
  2268. struct drm_i915_private *dev_priv =
  2269. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2270. intel_mark_idle(dev_priv->dev);
  2271. }
  2272. /**
  2273. * Ensures that an object will eventually get non-busy by flushing any required
  2274. * write domains, emitting any outstanding lazy request and retiring and
  2275. * completed requests.
  2276. */
  2277. static int
  2278. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2279. {
  2280. int ret;
  2281. if (obj->active) {
  2282. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2283. if (ret)
  2284. return ret;
  2285. i915_gem_retire_requests_ring(obj->ring);
  2286. }
  2287. return 0;
  2288. }
  2289. /**
  2290. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2291. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2292. *
  2293. * Returns 0 if successful, else an error is returned with the remaining time in
  2294. * the timeout parameter.
  2295. * -ETIME: object is still busy after timeout
  2296. * -ERESTARTSYS: signal interrupted the wait
  2297. * -ENONENT: object doesn't exist
  2298. * Also possible, but rare:
  2299. * -EAGAIN: GPU wedged
  2300. * -ENOMEM: damn
  2301. * -ENODEV: Internal IRQ fail
  2302. * -E?: The add request failed
  2303. *
  2304. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2305. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2306. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2307. * without holding struct_mutex the object may become re-busied before this
  2308. * function completes. A similar but shorter * race condition exists in the busy
  2309. * ioctl
  2310. */
  2311. int
  2312. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2313. {
  2314. struct drm_i915_private *dev_priv = dev->dev_private;
  2315. struct drm_i915_gem_wait *args = data;
  2316. struct drm_i915_gem_object *obj;
  2317. struct intel_engine_cs *ring = NULL;
  2318. struct timespec timeout_stack, *timeout = NULL;
  2319. unsigned reset_counter;
  2320. u32 seqno = 0;
  2321. int ret = 0;
  2322. if (args->timeout_ns >= 0) {
  2323. timeout_stack = ns_to_timespec(args->timeout_ns);
  2324. timeout = &timeout_stack;
  2325. }
  2326. ret = i915_mutex_lock_interruptible(dev);
  2327. if (ret)
  2328. return ret;
  2329. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2330. if (&obj->base == NULL) {
  2331. mutex_unlock(&dev->struct_mutex);
  2332. return -ENOENT;
  2333. }
  2334. /* Need to make sure the object gets inactive eventually. */
  2335. ret = i915_gem_object_flush_active(obj);
  2336. if (ret)
  2337. goto out;
  2338. if (obj->active) {
  2339. seqno = obj->last_read_seqno;
  2340. ring = obj->ring;
  2341. }
  2342. if (seqno == 0)
  2343. goto out;
  2344. /* Do this after OLR check to make sure we make forward progress polling
  2345. * on this IOCTL with a 0 timeout (like busy ioctl)
  2346. */
  2347. if (!args->timeout_ns) {
  2348. ret = -ETIME;
  2349. goto out;
  2350. }
  2351. drm_gem_object_unreference(&obj->base);
  2352. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2353. mutex_unlock(&dev->struct_mutex);
  2354. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout, file->driver_priv);
  2355. if (timeout)
  2356. args->timeout_ns = timespec_to_ns(timeout);
  2357. return ret;
  2358. out:
  2359. drm_gem_object_unreference(&obj->base);
  2360. mutex_unlock(&dev->struct_mutex);
  2361. return ret;
  2362. }
  2363. /**
  2364. * i915_gem_object_sync - sync an object to a ring.
  2365. *
  2366. * @obj: object which may be in use on another ring.
  2367. * @to: ring we wish to use the object on. May be NULL.
  2368. *
  2369. * This code is meant to abstract object synchronization with the GPU.
  2370. * Calling with NULL implies synchronizing the object with the CPU
  2371. * rather than a particular GPU ring.
  2372. *
  2373. * Returns 0 if successful, else propagates up the lower layer error.
  2374. */
  2375. int
  2376. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2377. struct intel_engine_cs *to)
  2378. {
  2379. struct intel_engine_cs *from = obj->ring;
  2380. u32 seqno;
  2381. int ret, idx;
  2382. if (from == NULL || to == from)
  2383. return 0;
  2384. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2385. return i915_gem_object_wait_rendering(obj, false);
  2386. idx = intel_ring_sync_index(from, to);
  2387. seqno = obj->last_read_seqno;
  2388. /* Optimization: Avoid semaphore sync when we are sure we already
  2389. * waited for an object with higher seqno */
  2390. if (seqno <= from->semaphore.sync_seqno[idx])
  2391. return 0;
  2392. ret = i915_gem_check_olr(obj->ring, seqno);
  2393. if (ret)
  2394. return ret;
  2395. trace_i915_gem_ring_sync_to(from, to, seqno);
  2396. ret = to->semaphore.sync_to(to, from, seqno);
  2397. if (!ret)
  2398. /* We use last_read_seqno because sync_to()
  2399. * might have just caused seqno wrap under
  2400. * the radar.
  2401. */
  2402. from->semaphore.sync_seqno[idx] = obj->last_read_seqno;
  2403. return ret;
  2404. }
  2405. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2406. {
  2407. u32 old_write_domain, old_read_domains;
  2408. /* Force a pagefault for domain tracking on next user access */
  2409. i915_gem_release_mmap(obj);
  2410. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2411. return;
  2412. /* Wait for any direct GTT access to complete */
  2413. mb();
  2414. old_read_domains = obj->base.read_domains;
  2415. old_write_domain = obj->base.write_domain;
  2416. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2417. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2418. trace_i915_gem_object_change_domain(obj,
  2419. old_read_domains,
  2420. old_write_domain);
  2421. }
  2422. int i915_vma_unbind(struct i915_vma *vma)
  2423. {
  2424. struct drm_i915_gem_object *obj = vma->obj;
  2425. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2426. int ret;
  2427. if (list_empty(&vma->vma_link))
  2428. return 0;
  2429. if (!drm_mm_node_allocated(&vma->node)) {
  2430. i915_gem_vma_destroy(vma);
  2431. return 0;
  2432. }
  2433. if (vma->pin_count)
  2434. return -EBUSY;
  2435. BUG_ON(obj->pages == NULL);
  2436. ret = i915_gem_object_finish_gpu(obj);
  2437. if (ret)
  2438. return ret;
  2439. /* Continue on if we fail due to EIO, the GPU is hung so we
  2440. * should be safe and we need to cleanup or else we might
  2441. * cause memory corruption through use-after-free.
  2442. */
  2443. if (i915_is_ggtt(vma->vm)) {
  2444. i915_gem_object_finish_gtt(obj);
  2445. /* release the fence reg _after_ flushing */
  2446. ret = i915_gem_object_put_fence(obj);
  2447. if (ret)
  2448. return ret;
  2449. }
  2450. trace_i915_vma_unbind(vma);
  2451. vma->unbind_vma(vma);
  2452. list_del_init(&vma->mm_list);
  2453. if (i915_is_ggtt(vma->vm))
  2454. obj->map_and_fenceable = false;
  2455. drm_mm_remove_node(&vma->node);
  2456. i915_gem_vma_destroy(vma);
  2457. /* Since the unbound list is global, only move to that list if
  2458. * no more VMAs exist. */
  2459. if (list_empty(&obj->vma_list)) {
  2460. i915_gem_gtt_finish_object(obj);
  2461. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2462. }
  2463. /* And finally now the object is completely decoupled from this vma,
  2464. * we can drop its hold on the backing storage and allow it to be
  2465. * reaped by the shrinker.
  2466. */
  2467. i915_gem_object_unpin_pages(obj);
  2468. return 0;
  2469. }
  2470. int i915_gpu_idle(struct drm_device *dev)
  2471. {
  2472. struct drm_i915_private *dev_priv = dev->dev_private;
  2473. struct intel_engine_cs *ring;
  2474. int ret, i;
  2475. /* Flush everything onto the inactive list. */
  2476. for_each_ring(ring, dev_priv, i) {
  2477. ret = i915_switch_context(ring, ring->default_context);
  2478. if (ret)
  2479. return ret;
  2480. ret = intel_ring_idle(ring);
  2481. if (ret)
  2482. return ret;
  2483. }
  2484. return 0;
  2485. }
  2486. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2487. struct drm_i915_gem_object *obj)
  2488. {
  2489. struct drm_i915_private *dev_priv = dev->dev_private;
  2490. int fence_reg;
  2491. int fence_pitch_shift;
  2492. if (INTEL_INFO(dev)->gen >= 6) {
  2493. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2494. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2495. } else {
  2496. fence_reg = FENCE_REG_965_0;
  2497. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2498. }
  2499. fence_reg += reg * 8;
  2500. /* To w/a incoherency with non-atomic 64-bit register updates,
  2501. * we split the 64-bit update into two 32-bit writes. In order
  2502. * for a partial fence not to be evaluated between writes, we
  2503. * precede the update with write to turn off the fence register,
  2504. * and only enable the fence as the last step.
  2505. *
  2506. * For extra levels of paranoia, we make sure each step lands
  2507. * before applying the next step.
  2508. */
  2509. I915_WRITE(fence_reg, 0);
  2510. POSTING_READ(fence_reg);
  2511. if (obj) {
  2512. u32 size = i915_gem_obj_ggtt_size(obj);
  2513. uint64_t val;
  2514. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2515. 0xfffff000) << 32;
  2516. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2517. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2518. if (obj->tiling_mode == I915_TILING_Y)
  2519. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2520. val |= I965_FENCE_REG_VALID;
  2521. I915_WRITE(fence_reg + 4, val >> 32);
  2522. POSTING_READ(fence_reg + 4);
  2523. I915_WRITE(fence_reg + 0, val);
  2524. POSTING_READ(fence_reg);
  2525. } else {
  2526. I915_WRITE(fence_reg + 4, 0);
  2527. POSTING_READ(fence_reg + 4);
  2528. }
  2529. }
  2530. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2531. struct drm_i915_gem_object *obj)
  2532. {
  2533. struct drm_i915_private *dev_priv = dev->dev_private;
  2534. u32 val;
  2535. if (obj) {
  2536. u32 size = i915_gem_obj_ggtt_size(obj);
  2537. int pitch_val;
  2538. int tile_width;
  2539. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2540. (size & -size) != size ||
  2541. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2542. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2543. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2544. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2545. tile_width = 128;
  2546. else
  2547. tile_width = 512;
  2548. /* Note: pitch better be a power of two tile widths */
  2549. pitch_val = obj->stride / tile_width;
  2550. pitch_val = ffs(pitch_val) - 1;
  2551. val = i915_gem_obj_ggtt_offset(obj);
  2552. if (obj->tiling_mode == I915_TILING_Y)
  2553. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2554. val |= I915_FENCE_SIZE_BITS(size);
  2555. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2556. val |= I830_FENCE_REG_VALID;
  2557. } else
  2558. val = 0;
  2559. if (reg < 8)
  2560. reg = FENCE_REG_830_0 + reg * 4;
  2561. else
  2562. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2563. I915_WRITE(reg, val);
  2564. POSTING_READ(reg);
  2565. }
  2566. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2567. struct drm_i915_gem_object *obj)
  2568. {
  2569. struct drm_i915_private *dev_priv = dev->dev_private;
  2570. uint32_t val;
  2571. if (obj) {
  2572. u32 size = i915_gem_obj_ggtt_size(obj);
  2573. uint32_t pitch_val;
  2574. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2575. (size & -size) != size ||
  2576. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2577. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2578. i915_gem_obj_ggtt_offset(obj), size);
  2579. pitch_val = obj->stride / 128;
  2580. pitch_val = ffs(pitch_val) - 1;
  2581. val = i915_gem_obj_ggtt_offset(obj);
  2582. if (obj->tiling_mode == I915_TILING_Y)
  2583. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2584. val |= I830_FENCE_SIZE_BITS(size);
  2585. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2586. val |= I830_FENCE_REG_VALID;
  2587. } else
  2588. val = 0;
  2589. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2590. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2591. }
  2592. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2593. {
  2594. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2595. }
  2596. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2597. struct drm_i915_gem_object *obj)
  2598. {
  2599. struct drm_i915_private *dev_priv = dev->dev_private;
  2600. /* Ensure that all CPU reads are completed before installing a fence
  2601. * and all writes before removing the fence.
  2602. */
  2603. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2604. mb();
  2605. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2606. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2607. obj->stride, obj->tiling_mode);
  2608. switch (INTEL_INFO(dev)->gen) {
  2609. case 8:
  2610. case 7:
  2611. case 6:
  2612. case 5:
  2613. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2614. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2615. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2616. default: BUG();
  2617. }
  2618. /* And similarly be paranoid that no direct access to this region
  2619. * is reordered to before the fence is installed.
  2620. */
  2621. if (i915_gem_object_needs_mb(obj))
  2622. mb();
  2623. }
  2624. static inline int fence_number(struct drm_i915_private *dev_priv,
  2625. struct drm_i915_fence_reg *fence)
  2626. {
  2627. return fence - dev_priv->fence_regs;
  2628. }
  2629. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2630. struct drm_i915_fence_reg *fence,
  2631. bool enable)
  2632. {
  2633. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2634. int reg = fence_number(dev_priv, fence);
  2635. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2636. if (enable) {
  2637. obj->fence_reg = reg;
  2638. fence->obj = obj;
  2639. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2640. } else {
  2641. obj->fence_reg = I915_FENCE_REG_NONE;
  2642. fence->obj = NULL;
  2643. list_del_init(&fence->lru_list);
  2644. }
  2645. obj->fence_dirty = false;
  2646. }
  2647. static int
  2648. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2649. {
  2650. if (obj->last_fenced_seqno) {
  2651. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2652. if (ret)
  2653. return ret;
  2654. obj->last_fenced_seqno = 0;
  2655. }
  2656. return 0;
  2657. }
  2658. int
  2659. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2660. {
  2661. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2662. struct drm_i915_fence_reg *fence;
  2663. int ret;
  2664. ret = i915_gem_object_wait_fence(obj);
  2665. if (ret)
  2666. return ret;
  2667. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2668. return 0;
  2669. fence = &dev_priv->fence_regs[obj->fence_reg];
  2670. if (WARN_ON(fence->pin_count))
  2671. return -EBUSY;
  2672. i915_gem_object_fence_lost(obj);
  2673. i915_gem_object_update_fence(obj, fence, false);
  2674. return 0;
  2675. }
  2676. static struct drm_i915_fence_reg *
  2677. i915_find_fence_reg(struct drm_device *dev)
  2678. {
  2679. struct drm_i915_private *dev_priv = dev->dev_private;
  2680. struct drm_i915_fence_reg *reg, *avail;
  2681. int i;
  2682. /* First try to find a free reg */
  2683. avail = NULL;
  2684. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2685. reg = &dev_priv->fence_regs[i];
  2686. if (!reg->obj)
  2687. return reg;
  2688. if (!reg->pin_count)
  2689. avail = reg;
  2690. }
  2691. if (avail == NULL)
  2692. goto deadlock;
  2693. /* None available, try to steal one or wait for a user to finish */
  2694. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2695. if (reg->pin_count)
  2696. continue;
  2697. return reg;
  2698. }
  2699. deadlock:
  2700. /* Wait for completion of pending flips which consume fences */
  2701. if (intel_has_pending_fb_unpin(dev))
  2702. return ERR_PTR(-EAGAIN);
  2703. return ERR_PTR(-EDEADLK);
  2704. }
  2705. /**
  2706. * i915_gem_object_get_fence - set up fencing for an object
  2707. * @obj: object to map through a fence reg
  2708. *
  2709. * When mapping objects through the GTT, userspace wants to be able to write
  2710. * to them without having to worry about swizzling if the object is tiled.
  2711. * This function walks the fence regs looking for a free one for @obj,
  2712. * stealing one if it can't find any.
  2713. *
  2714. * It then sets up the reg based on the object's properties: address, pitch
  2715. * and tiling format.
  2716. *
  2717. * For an untiled surface, this removes any existing fence.
  2718. */
  2719. int
  2720. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2721. {
  2722. struct drm_device *dev = obj->base.dev;
  2723. struct drm_i915_private *dev_priv = dev->dev_private;
  2724. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2725. struct drm_i915_fence_reg *reg;
  2726. int ret;
  2727. /* Have we updated the tiling parameters upon the object and so
  2728. * will need to serialise the write to the associated fence register?
  2729. */
  2730. if (obj->fence_dirty) {
  2731. ret = i915_gem_object_wait_fence(obj);
  2732. if (ret)
  2733. return ret;
  2734. }
  2735. /* Just update our place in the LRU if our fence is getting reused. */
  2736. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2737. reg = &dev_priv->fence_regs[obj->fence_reg];
  2738. if (!obj->fence_dirty) {
  2739. list_move_tail(&reg->lru_list,
  2740. &dev_priv->mm.fence_list);
  2741. return 0;
  2742. }
  2743. } else if (enable) {
  2744. if (WARN_ON(!obj->map_and_fenceable))
  2745. return -EINVAL;
  2746. reg = i915_find_fence_reg(dev);
  2747. if (IS_ERR(reg))
  2748. return PTR_ERR(reg);
  2749. if (reg->obj) {
  2750. struct drm_i915_gem_object *old = reg->obj;
  2751. ret = i915_gem_object_wait_fence(old);
  2752. if (ret)
  2753. return ret;
  2754. i915_gem_object_fence_lost(old);
  2755. }
  2756. } else
  2757. return 0;
  2758. i915_gem_object_update_fence(obj, reg, enable);
  2759. return 0;
  2760. }
  2761. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2762. struct drm_mm_node *gtt_space,
  2763. unsigned long cache_level)
  2764. {
  2765. struct drm_mm_node *other;
  2766. /* On non-LLC machines we have to be careful when putting differing
  2767. * types of snoopable memory together to avoid the prefetcher
  2768. * crossing memory domains and dying.
  2769. */
  2770. if (HAS_LLC(dev))
  2771. return true;
  2772. if (!drm_mm_node_allocated(gtt_space))
  2773. return true;
  2774. if (list_empty(&gtt_space->node_list))
  2775. return true;
  2776. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2777. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2778. return false;
  2779. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2780. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2781. return false;
  2782. return true;
  2783. }
  2784. static void i915_gem_verify_gtt(struct drm_device *dev)
  2785. {
  2786. #if WATCH_GTT
  2787. struct drm_i915_private *dev_priv = dev->dev_private;
  2788. struct drm_i915_gem_object *obj;
  2789. int err = 0;
  2790. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2791. if (obj->gtt_space == NULL) {
  2792. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2793. err++;
  2794. continue;
  2795. }
  2796. if (obj->cache_level != obj->gtt_space->color) {
  2797. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2798. i915_gem_obj_ggtt_offset(obj),
  2799. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2800. obj->cache_level,
  2801. obj->gtt_space->color);
  2802. err++;
  2803. continue;
  2804. }
  2805. if (!i915_gem_valid_gtt_space(dev,
  2806. obj->gtt_space,
  2807. obj->cache_level)) {
  2808. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2809. i915_gem_obj_ggtt_offset(obj),
  2810. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2811. obj->cache_level);
  2812. err++;
  2813. continue;
  2814. }
  2815. }
  2816. WARN_ON(err);
  2817. #endif
  2818. }
  2819. /**
  2820. * Finds free space in the GTT aperture and binds the object there.
  2821. */
  2822. static struct i915_vma *
  2823. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2824. struct i915_address_space *vm,
  2825. unsigned alignment,
  2826. uint64_t flags)
  2827. {
  2828. struct drm_device *dev = obj->base.dev;
  2829. struct drm_i915_private *dev_priv = dev->dev_private;
  2830. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2831. unsigned long start =
  2832. flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2833. unsigned long end =
  2834. flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
  2835. struct i915_vma *vma;
  2836. int ret;
  2837. fence_size = i915_gem_get_gtt_size(dev,
  2838. obj->base.size,
  2839. obj->tiling_mode);
  2840. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2841. obj->base.size,
  2842. obj->tiling_mode, true);
  2843. unfenced_alignment =
  2844. i915_gem_get_gtt_alignment(dev,
  2845. obj->base.size,
  2846. obj->tiling_mode, false);
  2847. if (alignment == 0)
  2848. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2849. unfenced_alignment;
  2850. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2851. DRM_DEBUG("Invalid object alignment requested %u\n", alignment);
  2852. return ERR_PTR(-EINVAL);
  2853. }
  2854. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2855. /* If the object is bigger than the entire aperture, reject it early
  2856. * before evicting everything in a vain attempt to find space.
  2857. */
  2858. if (obj->base.size > end) {
  2859. DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%lu\n",
  2860. obj->base.size,
  2861. flags & PIN_MAPPABLE ? "mappable" : "total",
  2862. end);
  2863. return ERR_PTR(-E2BIG);
  2864. }
  2865. ret = i915_gem_object_get_pages(obj);
  2866. if (ret)
  2867. return ERR_PTR(ret);
  2868. i915_gem_object_pin_pages(obj);
  2869. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  2870. if (IS_ERR(vma))
  2871. goto err_unpin;
  2872. search_free:
  2873. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2874. size, alignment,
  2875. obj->cache_level,
  2876. start, end,
  2877. DRM_MM_SEARCH_DEFAULT,
  2878. DRM_MM_CREATE_DEFAULT);
  2879. if (ret) {
  2880. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2881. obj->cache_level,
  2882. start, end,
  2883. flags);
  2884. if (ret == 0)
  2885. goto search_free;
  2886. goto err_free_vma;
  2887. }
  2888. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2889. obj->cache_level))) {
  2890. ret = -EINVAL;
  2891. goto err_remove_node;
  2892. }
  2893. ret = i915_gem_gtt_prepare_object(obj);
  2894. if (ret)
  2895. goto err_remove_node;
  2896. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2897. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2898. if (i915_is_ggtt(vm)) {
  2899. bool mappable, fenceable;
  2900. fenceable = (vma->node.size == fence_size &&
  2901. (vma->node.start & (fence_alignment - 1)) == 0);
  2902. mappable = (vma->node.start + obj->base.size <=
  2903. dev_priv->gtt.mappable_end);
  2904. obj->map_and_fenceable = mappable && fenceable;
  2905. }
  2906. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  2907. trace_i915_vma_bind(vma, flags);
  2908. vma->bind_vma(vma, obj->cache_level,
  2909. flags & (PIN_MAPPABLE | PIN_GLOBAL) ? GLOBAL_BIND : 0);
  2910. i915_gem_verify_gtt(dev);
  2911. return vma;
  2912. err_remove_node:
  2913. drm_mm_remove_node(&vma->node);
  2914. err_free_vma:
  2915. i915_gem_vma_destroy(vma);
  2916. vma = ERR_PTR(ret);
  2917. err_unpin:
  2918. i915_gem_object_unpin_pages(obj);
  2919. return vma;
  2920. }
  2921. bool
  2922. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2923. bool force)
  2924. {
  2925. /* If we don't have a page list set up, then we're not pinned
  2926. * to GPU, and we can ignore the cache flush because it'll happen
  2927. * again at bind time.
  2928. */
  2929. if (obj->pages == NULL)
  2930. return false;
  2931. /*
  2932. * Stolen memory is always coherent with the GPU as it is explicitly
  2933. * marked as wc by the system, or the system is cache-coherent.
  2934. */
  2935. if (obj->stolen)
  2936. return false;
  2937. /* If the GPU is snooping the contents of the CPU cache,
  2938. * we do not need to manually clear the CPU cache lines. However,
  2939. * the caches are only snooped when the render cache is
  2940. * flushed/invalidated. As we always have to emit invalidations
  2941. * and flushes when moving into and out of the RENDER domain, correct
  2942. * snooping behaviour occurs naturally as the result of our domain
  2943. * tracking.
  2944. */
  2945. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  2946. return false;
  2947. trace_i915_gem_object_clflush(obj);
  2948. drm_clflush_sg(obj->pages);
  2949. return true;
  2950. }
  2951. /** Flushes the GTT write domain for the object if it's dirty. */
  2952. static void
  2953. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2954. {
  2955. uint32_t old_write_domain;
  2956. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2957. return;
  2958. /* No actual flushing is required for the GTT write domain. Writes
  2959. * to it immediately go to main memory as far as we know, so there's
  2960. * no chipset flush. It also doesn't land in render cache.
  2961. *
  2962. * However, we do have to enforce the order so that all writes through
  2963. * the GTT land before any writes to the device, such as updates to
  2964. * the GATT itself.
  2965. */
  2966. wmb();
  2967. old_write_domain = obj->base.write_domain;
  2968. obj->base.write_domain = 0;
  2969. intel_fb_obj_flush(obj, false);
  2970. trace_i915_gem_object_change_domain(obj,
  2971. obj->base.read_domains,
  2972. old_write_domain);
  2973. }
  2974. /** Flushes the CPU write domain for the object if it's dirty. */
  2975. static void
  2976. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  2977. bool force)
  2978. {
  2979. uint32_t old_write_domain;
  2980. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2981. return;
  2982. if (i915_gem_clflush_object(obj, force))
  2983. i915_gem_chipset_flush(obj->base.dev);
  2984. old_write_domain = obj->base.write_domain;
  2985. obj->base.write_domain = 0;
  2986. intel_fb_obj_flush(obj, false);
  2987. trace_i915_gem_object_change_domain(obj,
  2988. obj->base.read_domains,
  2989. old_write_domain);
  2990. }
  2991. /**
  2992. * Moves a single object to the GTT read, and possibly write domain.
  2993. *
  2994. * This function returns when the move is complete, including waiting on
  2995. * flushes to occur.
  2996. */
  2997. int
  2998. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2999. {
  3000. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3001. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3002. uint32_t old_write_domain, old_read_domains;
  3003. int ret;
  3004. /* Not valid to be called on unbound objects. */
  3005. if (vma == NULL)
  3006. return -EINVAL;
  3007. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3008. return 0;
  3009. ret = i915_gem_object_wait_rendering(obj, !write);
  3010. if (ret)
  3011. return ret;
  3012. i915_gem_object_retire(obj);
  3013. i915_gem_object_flush_cpu_write_domain(obj, false);
  3014. /* Serialise direct access to this object with the barriers for
  3015. * coherent writes from the GPU, by effectively invalidating the
  3016. * GTT domain upon first access.
  3017. */
  3018. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3019. mb();
  3020. old_write_domain = obj->base.write_domain;
  3021. old_read_domains = obj->base.read_domains;
  3022. /* It should now be out of any other write domains, and we can update
  3023. * the domain values for our changes.
  3024. */
  3025. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3026. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3027. if (write) {
  3028. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3029. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3030. obj->dirty = 1;
  3031. }
  3032. if (write)
  3033. intel_fb_obj_invalidate(obj, NULL);
  3034. trace_i915_gem_object_change_domain(obj,
  3035. old_read_domains,
  3036. old_write_domain);
  3037. /* And bump the LRU for this access */
  3038. if (i915_gem_object_is_inactive(obj))
  3039. list_move_tail(&vma->mm_list,
  3040. &dev_priv->gtt.base.inactive_list);
  3041. return 0;
  3042. }
  3043. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3044. enum i915_cache_level cache_level)
  3045. {
  3046. struct drm_device *dev = obj->base.dev;
  3047. struct i915_vma *vma, *next;
  3048. int ret;
  3049. if (obj->cache_level == cache_level)
  3050. return 0;
  3051. if (i915_gem_obj_is_pinned(obj)) {
  3052. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3053. return -EBUSY;
  3054. }
  3055. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3056. if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  3057. ret = i915_vma_unbind(vma);
  3058. if (ret)
  3059. return ret;
  3060. }
  3061. }
  3062. if (i915_gem_obj_bound_any(obj)) {
  3063. ret = i915_gem_object_finish_gpu(obj);
  3064. if (ret)
  3065. return ret;
  3066. i915_gem_object_finish_gtt(obj);
  3067. /* Before SandyBridge, you could not use tiling or fence
  3068. * registers with snooped memory, so relinquish any fences
  3069. * currently pointing to our region in the aperture.
  3070. */
  3071. if (INTEL_INFO(dev)->gen < 6) {
  3072. ret = i915_gem_object_put_fence(obj);
  3073. if (ret)
  3074. return ret;
  3075. }
  3076. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3077. if (drm_mm_node_allocated(&vma->node))
  3078. vma->bind_vma(vma, cache_level,
  3079. obj->has_global_gtt_mapping ? GLOBAL_BIND : 0);
  3080. }
  3081. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3082. vma->node.color = cache_level;
  3083. obj->cache_level = cache_level;
  3084. if (cpu_write_needs_clflush(obj)) {
  3085. u32 old_read_domains, old_write_domain;
  3086. /* If we're coming from LLC cached, then we haven't
  3087. * actually been tracking whether the data is in the
  3088. * CPU cache or not, since we only allow one bit set
  3089. * in obj->write_domain and have been skipping the clflushes.
  3090. * Just set it to the CPU cache for now.
  3091. */
  3092. i915_gem_object_retire(obj);
  3093. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3094. old_read_domains = obj->base.read_domains;
  3095. old_write_domain = obj->base.write_domain;
  3096. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3097. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3098. trace_i915_gem_object_change_domain(obj,
  3099. old_read_domains,
  3100. old_write_domain);
  3101. }
  3102. i915_gem_verify_gtt(dev);
  3103. return 0;
  3104. }
  3105. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3106. struct drm_file *file)
  3107. {
  3108. struct drm_i915_gem_caching *args = data;
  3109. struct drm_i915_gem_object *obj;
  3110. int ret;
  3111. ret = i915_mutex_lock_interruptible(dev);
  3112. if (ret)
  3113. return ret;
  3114. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3115. if (&obj->base == NULL) {
  3116. ret = -ENOENT;
  3117. goto unlock;
  3118. }
  3119. switch (obj->cache_level) {
  3120. case I915_CACHE_LLC:
  3121. case I915_CACHE_L3_LLC:
  3122. args->caching = I915_CACHING_CACHED;
  3123. break;
  3124. case I915_CACHE_WT:
  3125. args->caching = I915_CACHING_DISPLAY;
  3126. break;
  3127. default:
  3128. args->caching = I915_CACHING_NONE;
  3129. break;
  3130. }
  3131. drm_gem_object_unreference(&obj->base);
  3132. unlock:
  3133. mutex_unlock(&dev->struct_mutex);
  3134. return ret;
  3135. }
  3136. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3137. struct drm_file *file)
  3138. {
  3139. struct drm_i915_gem_caching *args = data;
  3140. struct drm_i915_gem_object *obj;
  3141. enum i915_cache_level level;
  3142. int ret;
  3143. switch (args->caching) {
  3144. case I915_CACHING_NONE:
  3145. level = I915_CACHE_NONE;
  3146. break;
  3147. case I915_CACHING_CACHED:
  3148. level = I915_CACHE_LLC;
  3149. break;
  3150. case I915_CACHING_DISPLAY:
  3151. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3152. break;
  3153. default:
  3154. return -EINVAL;
  3155. }
  3156. ret = i915_mutex_lock_interruptible(dev);
  3157. if (ret)
  3158. return ret;
  3159. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3160. if (&obj->base == NULL) {
  3161. ret = -ENOENT;
  3162. goto unlock;
  3163. }
  3164. ret = i915_gem_object_set_cache_level(obj, level);
  3165. drm_gem_object_unreference(&obj->base);
  3166. unlock:
  3167. mutex_unlock(&dev->struct_mutex);
  3168. return ret;
  3169. }
  3170. static bool is_pin_display(struct drm_i915_gem_object *obj)
  3171. {
  3172. struct i915_vma *vma;
  3173. vma = i915_gem_obj_to_ggtt(obj);
  3174. if (!vma)
  3175. return false;
  3176. /* There are 3 sources that pin objects:
  3177. * 1. The display engine (scanouts, sprites, cursors);
  3178. * 2. Reservations for execbuffer;
  3179. * 3. The user.
  3180. *
  3181. * We can ignore reservations as we hold the struct_mutex and
  3182. * are only called outside of the reservation path. The user
  3183. * can only increment pin_count once, and so if after
  3184. * subtracting the potential reference by the user, any pin_count
  3185. * remains, it must be due to another use by the display engine.
  3186. */
  3187. return vma->pin_count - !!obj->user_pin_count;
  3188. }
  3189. /*
  3190. * Prepare buffer for display plane (scanout, cursors, etc).
  3191. * Can be called from an uninterruptible phase (modesetting) and allows
  3192. * any flushes to be pipelined (for pageflips).
  3193. */
  3194. int
  3195. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3196. u32 alignment,
  3197. struct intel_engine_cs *pipelined)
  3198. {
  3199. u32 old_read_domains, old_write_domain;
  3200. bool was_pin_display;
  3201. int ret;
  3202. if (pipelined != obj->ring) {
  3203. ret = i915_gem_object_sync(obj, pipelined);
  3204. if (ret)
  3205. return ret;
  3206. }
  3207. /* Mark the pin_display early so that we account for the
  3208. * display coherency whilst setting up the cache domains.
  3209. */
  3210. was_pin_display = obj->pin_display;
  3211. obj->pin_display = true;
  3212. /* The display engine is not coherent with the LLC cache on gen6. As
  3213. * a result, we make sure that the pinning that is about to occur is
  3214. * done with uncached PTEs. This is lowest common denominator for all
  3215. * chipsets.
  3216. *
  3217. * However for gen6+, we could do better by using the GFDT bit instead
  3218. * of uncaching, which would allow us to flush all the LLC-cached data
  3219. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3220. */
  3221. ret = i915_gem_object_set_cache_level(obj,
  3222. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3223. if (ret)
  3224. goto err_unpin_display;
  3225. /* As the user may map the buffer once pinned in the display plane
  3226. * (e.g. libkms for the bootup splash), we have to ensure that we
  3227. * always use map_and_fenceable for all scanout buffers.
  3228. */
  3229. ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE);
  3230. if (ret)
  3231. goto err_unpin_display;
  3232. i915_gem_object_flush_cpu_write_domain(obj, true);
  3233. old_write_domain = obj->base.write_domain;
  3234. old_read_domains = obj->base.read_domains;
  3235. /* It should now be out of any other write domains, and we can update
  3236. * the domain values for our changes.
  3237. */
  3238. obj->base.write_domain = 0;
  3239. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3240. trace_i915_gem_object_change_domain(obj,
  3241. old_read_domains,
  3242. old_write_domain);
  3243. return 0;
  3244. err_unpin_display:
  3245. WARN_ON(was_pin_display != is_pin_display(obj));
  3246. obj->pin_display = was_pin_display;
  3247. return ret;
  3248. }
  3249. void
  3250. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
  3251. {
  3252. i915_gem_object_ggtt_unpin(obj);
  3253. obj->pin_display = is_pin_display(obj);
  3254. }
  3255. int
  3256. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  3257. {
  3258. int ret;
  3259. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  3260. return 0;
  3261. ret = i915_gem_object_wait_rendering(obj, false);
  3262. if (ret)
  3263. return ret;
  3264. /* Ensure that we invalidate the GPU's caches and TLBs. */
  3265. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  3266. return 0;
  3267. }
  3268. /**
  3269. * Moves a single object to the CPU read, and possibly write domain.
  3270. *
  3271. * This function returns when the move is complete, including waiting on
  3272. * flushes to occur.
  3273. */
  3274. int
  3275. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3276. {
  3277. uint32_t old_write_domain, old_read_domains;
  3278. int ret;
  3279. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3280. return 0;
  3281. ret = i915_gem_object_wait_rendering(obj, !write);
  3282. if (ret)
  3283. return ret;
  3284. i915_gem_object_retire(obj);
  3285. i915_gem_object_flush_gtt_write_domain(obj);
  3286. old_write_domain = obj->base.write_domain;
  3287. old_read_domains = obj->base.read_domains;
  3288. /* Flush the CPU cache if it's still invalid. */
  3289. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3290. i915_gem_clflush_object(obj, false);
  3291. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3292. }
  3293. /* It should now be out of any other write domains, and we can update
  3294. * the domain values for our changes.
  3295. */
  3296. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3297. /* If we're writing through the CPU, then the GPU read domains will
  3298. * need to be invalidated at next use.
  3299. */
  3300. if (write) {
  3301. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3302. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3303. }
  3304. if (write)
  3305. intel_fb_obj_invalidate(obj, NULL);
  3306. trace_i915_gem_object_change_domain(obj,
  3307. old_read_domains,
  3308. old_write_domain);
  3309. return 0;
  3310. }
  3311. /* Throttle our rendering by waiting until the ring has completed our requests
  3312. * emitted over 20 msec ago.
  3313. *
  3314. * Note that if we were to use the current jiffies each time around the loop,
  3315. * we wouldn't escape the function with any frames outstanding if the time to
  3316. * render a frame was over 20ms.
  3317. *
  3318. * This should get us reasonable parallelism between CPU and GPU but also
  3319. * relatively low latency when blocking on a particular request to finish.
  3320. */
  3321. static int
  3322. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3323. {
  3324. struct drm_i915_private *dev_priv = dev->dev_private;
  3325. struct drm_i915_file_private *file_priv = file->driver_priv;
  3326. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3327. struct drm_i915_gem_request *request;
  3328. struct intel_engine_cs *ring = NULL;
  3329. unsigned reset_counter;
  3330. u32 seqno = 0;
  3331. int ret;
  3332. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3333. if (ret)
  3334. return ret;
  3335. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3336. if (ret)
  3337. return ret;
  3338. spin_lock(&file_priv->mm.lock);
  3339. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3340. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3341. break;
  3342. ring = request->ring;
  3343. seqno = request->seqno;
  3344. }
  3345. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3346. spin_unlock(&file_priv->mm.lock);
  3347. if (seqno == 0)
  3348. return 0;
  3349. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, NULL);
  3350. if (ret == 0)
  3351. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3352. return ret;
  3353. }
  3354. static bool
  3355. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3356. {
  3357. struct drm_i915_gem_object *obj = vma->obj;
  3358. if (alignment &&
  3359. vma->node.start & (alignment - 1))
  3360. return true;
  3361. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3362. return true;
  3363. if (flags & PIN_OFFSET_BIAS &&
  3364. vma->node.start < (flags & PIN_OFFSET_MASK))
  3365. return true;
  3366. return false;
  3367. }
  3368. int
  3369. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3370. struct i915_address_space *vm,
  3371. uint32_t alignment,
  3372. uint64_t flags)
  3373. {
  3374. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3375. struct i915_vma *vma;
  3376. int ret;
  3377. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3378. return -ENODEV;
  3379. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3380. return -EINVAL;
  3381. vma = i915_gem_obj_to_vma(obj, vm);
  3382. if (vma) {
  3383. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3384. return -EBUSY;
  3385. if (i915_vma_misplaced(vma, alignment, flags)) {
  3386. WARN(vma->pin_count,
  3387. "bo is already pinned with incorrect alignment:"
  3388. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3389. " obj->map_and_fenceable=%d\n",
  3390. i915_gem_obj_offset(obj, vm), alignment,
  3391. !!(flags & PIN_MAPPABLE),
  3392. obj->map_and_fenceable);
  3393. ret = i915_vma_unbind(vma);
  3394. if (ret)
  3395. return ret;
  3396. vma = NULL;
  3397. }
  3398. }
  3399. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3400. vma = i915_gem_object_bind_to_vm(obj, vm, alignment, flags);
  3401. if (IS_ERR(vma))
  3402. return PTR_ERR(vma);
  3403. }
  3404. if (flags & PIN_GLOBAL && !obj->has_global_gtt_mapping)
  3405. vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
  3406. vma->pin_count++;
  3407. if (flags & PIN_MAPPABLE)
  3408. obj->pin_mappable |= true;
  3409. return 0;
  3410. }
  3411. void
  3412. i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj)
  3413. {
  3414. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3415. BUG_ON(!vma);
  3416. BUG_ON(vma->pin_count == 0);
  3417. BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3418. if (--vma->pin_count == 0)
  3419. obj->pin_mappable = false;
  3420. }
  3421. bool
  3422. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  3423. {
  3424. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3425. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3426. struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  3427. WARN_ON(!ggtt_vma ||
  3428. dev_priv->fence_regs[obj->fence_reg].pin_count >
  3429. ggtt_vma->pin_count);
  3430. dev_priv->fence_regs[obj->fence_reg].pin_count++;
  3431. return true;
  3432. } else
  3433. return false;
  3434. }
  3435. void
  3436. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  3437. {
  3438. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3439. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3440. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  3441. dev_priv->fence_regs[obj->fence_reg].pin_count--;
  3442. }
  3443. }
  3444. int
  3445. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3446. struct drm_file *file)
  3447. {
  3448. struct drm_i915_gem_pin *args = data;
  3449. struct drm_i915_gem_object *obj;
  3450. int ret;
  3451. if (INTEL_INFO(dev)->gen >= 6)
  3452. return -ENODEV;
  3453. ret = i915_mutex_lock_interruptible(dev);
  3454. if (ret)
  3455. return ret;
  3456. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3457. if (&obj->base == NULL) {
  3458. ret = -ENOENT;
  3459. goto unlock;
  3460. }
  3461. if (obj->madv != I915_MADV_WILLNEED) {
  3462. DRM_DEBUG("Attempting to pin a purgeable buffer\n");
  3463. ret = -EFAULT;
  3464. goto out;
  3465. }
  3466. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3467. DRM_DEBUG("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3468. args->handle);
  3469. ret = -EINVAL;
  3470. goto out;
  3471. }
  3472. if (obj->user_pin_count == ULONG_MAX) {
  3473. ret = -EBUSY;
  3474. goto out;
  3475. }
  3476. if (obj->user_pin_count == 0) {
  3477. ret = i915_gem_obj_ggtt_pin(obj, args->alignment, PIN_MAPPABLE);
  3478. if (ret)
  3479. goto out;
  3480. }
  3481. obj->user_pin_count++;
  3482. obj->pin_filp = file;
  3483. args->offset = i915_gem_obj_ggtt_offset(obj);
  3484. out:
  3485. drm_gem_object_unreference(&obj->base);
  3486. unlock:
  3487. mutex_unlock(&dev->struct_mutex);
  3488. return ret;
  3489. }
  3490. int
  3491. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3492. struct drm_file *file)
  3493. {
  3494. struct drm_i915_gem_pin *args = data;
  3495. struct drm_i915_gem_object *obj;
  3496. int ret;
  3497. ret = i915_mutex_lock_interruptible(dev);
  3498. if (ret)
  3499. return ret;
  3500. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3501. if (&obj->base == NULL) {
  3502. ret = -ENOENT;
  3503. goto unlock;
  3504. }
  3505. if (obj->pin_filp != file) {
  3506. DRM_DEBUG("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3507. args->handle);
  3508. ret = -EINVAL;
  3509. goto out;
  3510. }
  3511. obj->user_pin_count--;
  3512. if (obj->user_pin_count == 0) {
  3513. obj->pin_filp = NULL;
  3514. i915_gem_object_ggtt_unpin(obj);
  3515. }
  3516. out:
  3517. drm_gem_object_unreference(&obj->base);
  3518. unlock:
  3519. mutex_unlock(&dev->struct_mutex);
  3520. return ret;
  3521. }
  3522. int
  3523. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3524. struct drm_file *file)
  3525. {
  3526. struct drm_i915_gem_busy *args = data;
  3527. struct drm_i915_gem_object *obj;
  3528. int ret;
  3529. ret = i915_mutex_lock_interruptible(dev);
  3530. if (ret)
  3531. return ret;
  3532. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3533. if (&obj->base == NULL) {
  3534. ret = -ENOENT;
  3535. goto unlock;
  3536. }
  3537. /* Count all active objects as busy, even if they are currently not used
  3538. * by the gpu. Users of this interface expect objects to eventually
  3539. * become non-busy without any further actions, therefore emit any
  3540. * necessary flushes here.
  3541. */
  3542. ret = i915_gem_object_flush_active(obj);
  3543. args->busy = obj->active;
  3544. if (obj->ring) {
  3545. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3546. args->busy |= intel_ring_flag(obj->ring) << 16;
  3547. }
  3548. drm_gem_object_unreference(&obj->base);
  3549. unlock:
  3550. mutex_unlock(&dev->struct_mutex);
  3551. return ret;
  3552. }
  3553. int
  3554. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3555. struct drm_file *file_priv)
  3556. {
  3557. return i915_gem_ring_throttle(dev, file_priv);
  3558. }
  3559. int
  3560. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3561. struct drm_file *file_priv)
  3562. {
  3563. struct drm_i915_gem_madvise *args = data;
  3564. struct drm_i915_gem_object *obj;
  3565. int ret;
  3566. switch (args->madv) {
  3567. case I915_MADV_DONTNEED:
  3568. case I915_MADV_WILLNEED:
  3569. break;
  3570. default:
  3571. return -EINVAL;
  3572. }
  3573. ret = i915_mutex_lock_interruptible(dev);
  3574. if (ret)
  3575. return ret;
  3576. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3577. if (&obj->base == NULL) {
  3578. ret = -ENOENT;
  3579. goto unlock;
  3580. }
  3581. if (i915_gem_obj_is_pinned(obj)) {
  3582. ret = -EINVAL;
  3583. goto out;
  3584. }
  3585. if (obj->madv != __I915_MADV_PURGED)
  3586. obj->madv = args->madv;
  3587. /* if the object is no longer attached, discard its backing storage */
  3588. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3589. i915_gem_object_truncate(obj);
  3590. args->retained = obj->madv != __I915_MADV_PURGED;
  3591. out:
  3592. drm_gem_object_unreference(&obj->base);
  3593. unlock:
  3594. mutex_unlock(&dev->struct_mutex);
  3595. return ret;
  3596. }
  3597. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3598. const struct drm_i915_gem_object_ops *ops)
  3599. {
  3600. INIT_LIST_HEAD(&obj->global_list);
  3601. INIT_LIST_HEAD(&obj->ring_list);
  3602. INIT_LIST_HEAD(&obj->obj_exec_link);
  3603. INIT_LIST_HEAD(&obj->vma_list);
  3604. obj->ops = ops;
  3605. obj->fence_reg = I915_FENCE_REG_NONE;
  3606. obj->madv = I915_MADV_WILLNEED;
  3607. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3608. }
  3609. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3610. .get_pages = i915_gem_object_get_pages_gtt,
  3611. .put_pages = i915_gem_object_put_pages_gtt,
  3612. };
  3613. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3614. size_t size)
  3615. {
  3616. struct drm_i915_gem_object *obj;
  3617. struct address_space *mapping;
  3618. gfp_t mask;
  3619. obj = i915_gem_object_alloc(dev);
  3620. if (obj == NULL)
  3621. return NULL;
  3622. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3623. i915_gem_object_free(obj);
  3624. return NULL;
  3625. }
  3626. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3627. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3628. /* 965gm cannot relocate objects above 4GiB. */
  3629. mask &= ~__GFP_HIGHMEM;
  3630. mask |= __GFP_DMA32;
  3631. }
  3632. mapping = file_inode(obj->base.filp)->i_mapping;
  3633. mapping_set_gfp_mask(mapping, mask);
  3634. i915_gem_object_init(obj, &i915_gem_object_ops);
  3635. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3636. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3637. if (HAS_LLC(dev)) {
  3638. /* On some devices, we can have the GPU use the LLC (the CPU
  3639. * cache) for about a 10% performance improvement
  3640. * compared to uncached. Graphics requests other than
  3641. * display scanout are coherent with the CPU in
  3642. * accessing this cache. This means in this mode we
  3643. * don't need to clflush on the CPU side, and on the
  3644. * GPU side we only need to flush internal caches to
  3645. * get data visible to the CPU.
  3646. *
  3647. * However, we maintain the display planes as UC, and so
  3648. * need to rebind when first used as such.
  3649. */
  3650. obj->cache_level = I915_CACHE_LLC;
  3651. } else
  3652. obj->cache_level = I915_CACHE_NONE;
  3653. trace_i915_gem_object_create(obj);
  3654. return obj;
  3655. }
  3656. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3657. {
  3658. /* If we are the last user of the backing storage (be it shmemfs
  3659. * pages or stolen etc), we know that the pages are going to be
  3660. * immediately released. In this case, we can then skip copying
  3661. * back the contents from the GPU.
  3662. */
  3663. if (obj->madv != I915_MADV_WILLNEED)
  3664. return false;
  3665. if (obj->base.filp == NULL)
  3666. return true;
  3667. /* At first glance, this looks racy, but then again so would be
  3668. * userspace racing mmap against close. However, the first external
  3669. * reference to the filp can only be obtained through the
  3670. * i915_gem_mmap_ioctl() which safeguards us against the user
  3671. * acquiring such a reference whilst we are in the middle of
  3672. * freeing the object.
  3673. */
  3674. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3675. }
  3676. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3677. {
  3678. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3679. struct drm_device *dev = obj->base.dev;
  3680. struct drm_i915_private *dev_priv = dev->dev_private;
  3681. struct i915_vma *vma, *next;
  3682. intel_runtime_pm_get(dev_priv);
  3683. trace_i915_gem_object_destroy(obj);
  3684. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3685. int ret;
  3686. vma->pin_count = 0;
  3687. ret = i915_vma_unbind(vma);
  3688. if (WARN_ON(ret == -ERESTARTSYS)) {
  3689. bool was_interruptible;
  3690. was_interruptible = dev_priv->mm.interruptible;
  3691. dev_priv->mm.interruptible = false;
  3692. WARN_ON(i915_vma_unbind(vma));
  3693. dev_priv->mm.interruptible = was_interruptible;
  3694. }
  3695. }
  3696. i915_gem_object_detach_phys(obj);
  3697. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3698. * before progressing. */
  3699. if (obj->stolen)
  3700. i915_gem_object_unpin_pages(obj);
  3701. WARN_ON(obj->frontbuffer_bits);
  3702. if (WARN_ON(obj->pages_pin_count))
  3703. obj->pages_pin_count = 0;
  3704. if (discard_backing_storage(obj))
  3705. obj->madv = I915_MADV_DONTNEED;
  3706. i915_gem_object_put_pages(obj);
  3707. i915_gem_object_free_mmap_offset(obj);
  3708. BUG_ON(obj->pages);
  3709. if (obj->base.import_attach)
  3710. drm_prime_gem_destroy(&obj->base, NULL);
  3711. if (obj->ops->release)
  3712. obj->ops->release(obj);
  3713. drm_gem_object_release(&obj->base);
  3714. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3715. kfree(obj->bit_17);
  3716. i915_gem_object_free(obj);
  3717. intel_runtime_pm_put(dev_priv);
  3718. }
  3719. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3720. struct i915_address_space *vm)
  3721. {
  3722. struct i915_vma *vma;
  3723. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3724. if (vma->vm == vm)
  3725. return vma;
  3726. return NULL;
  3727. }
  3728. void i915_gem_vma_destroy(struct i915_vma *vma)
  3729. {
  3730. struct i915_address_space *vm = NULL;
  3731. WARN_ON(vma->node.allocated);
  3732. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3733. if (!list_empty(&vma->exec_list))
  3734. return;
  3735. vm = vma->vm;
  3736. if (!i915_is_ggtt(vm))
  3737. i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  3738. list_del(&vma->vma_link);
  3739. kfree(vma);
  3740. }
  3741. static void
  3742. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3743. {
  3744. struct drm_i915_private *dev_priv = dev->dev_private;
  3745. struct intel_engine_cs *ring;
  3746. int i;
  3747. for_each_ring(ring, dev_priv, i)
  3748. dev_priv->gt.stop_ring(ring);
  3749. }
  3750. int
  3751. i915_gem_suspend(struct drm_device *dev)
  3752. {
  3753. struct drm_i915_private *dev_priv = dev->dev_private;
  3754. int ret = 0;
  3755. mutex_lock(&dev->struct_mutex);
  3756. if (dev_priv->ums.mm_suspended)
  3757. goto err;
  3758. ret = i915_gpu_idle(dev);
  3759. if (ret)
  3760. goto err;
  3761. i915_gem_retire_requests(dev);
  3762. /* Under UMS, be paranoid and evict. */
  3763. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3764. i915_gem_evict_everything(dev);
  3765. i915_kernel_lost_context(dev);
  3766. i915_gem_stop_ringbuffers(dev);
  3767. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3768. * We need to replace this with a semaphore, or something.
  3769. * And not confound ums.mm_suspended!
  3770. */
  3771. dev_priv->ums.mm_suspended = !drm_core_check_feature(dev,
  3772. DRIVER_MODESET);
  3773. mutex_unlock(&dev->struct_mutex);
  3774. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3775. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3776. flush_delayed_work(&dev_priv->mm.idle_work);
  3777. return 0;
  3778. err:
  3779. mutex_unlock(&dev->struct_mutex);
  3780. return ret;
  3781. }
  3782. int i915_gem_l3_remap(struct intel_engine_cs *ring, int slice)
  3783. {
  3784. struct drm_device *dev = ring->dev;
  3785. struct drm_i915_private *dev_priv = dev->dev_private;
  3786. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3787. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3788. int i, ret;
  3789. if (!HAS_L3_DPF(dev) || !remap_info)
  3790. return 0;
  3791. ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  3792. if (ret)
  3793. return ret;
  3794. /*
  3795. * Note: We do not worry about the concurrent register cacheline hang
  3796. * here because no other code should access these registers other than
  3797. * at initialization time.
  3798. */
  3799. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3800. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3801. intel_ring_emit(ring, reg_base + i);
  3802. intel_ring_emit(ring, remap_info[i/4]);
  3803. }
  3804. intel_ring_advance(ring);
  3805. return ret;
  3806. }
  3807. void i915_gem_init_swizzling(struct drm_device *dev)
  3808. {
  3809. struct drm_i915_private *dev_priv = dev->dev_private;
  3810. if (INTEL_INFO(dev)->gen < 5 ||
  3811. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3812. return;
  3813. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3814. DISP_TILE_SURFACE_SWIZZLING);
  3815. if (IS_GEN5(dev))
  3816. return;
  3817. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3818. if (IS_GEN6(dev))
  3819. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3820. else if (IS_GEN7(dev))
  3821. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3822. else if (IS_GEN8(dev))
  3823. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3824. else
  3825. BUG();
  3826. }
  3827. static bool
  3828. intel_enable_blt(struct drm_device *dev)
  3829. {
  3830. if (!HAS_BLT(dev))
  3831. return false;
  3832. /* The blitter was dysfunctional on early prototypes */
  3833. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3834. DRM_INFO("BLT not supported on this pre-production hardware;"
  3835. " graphics performance will be degraded.\n");
  3836. return false;
  3837. }
  3838. return true;
  3839. }
  3840. int i915_gem_init_rings(struct drm_device *dev)
  3841. {
  3842. struct drm_i915_private *dev_priv = dev->dev_private;
  3843. int ret;
  3844. ret = intel_init_render_ring_buffer(dev);
  3845. if (ret)
  3846. return ret;
  3847. if (HAS_BSD(dev)) {
  3848. ret = intel_init_bsd_ring_buffer(dev);
  3849. if (ret)
  3850. goto cleanup_render_ring;
  3851. }
  3852. if (intel_enable_blt(dev)) {
  3853. ret = intel_init_blt_ring_buffer(dev);
  3854. if (ret)
  3855. goto cleanup_bsd_ring;
  3856. }
  3857. if (HAS_VEBOX(dev)) {
  3858. ret = intel_init_vebox_ring_buffer(dev);
  3859. if (ret)
  3860. goto cleanup_blt_ring;
  3861. }
  3862. if (HAS_BSD2(dev)) {
  3863. ret = intel_init_bsd2_ring_buffer(dev);
  3864. if (ret)
  3865. goto cleanup_vebox_ring;
  3866. }
  3867. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3868. if (ret)
  3869. goto cleanup_bsd2_ring;
  3870. return 0;
  3871. cleanup_bsd2_ring:
  3872. intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
  3873. cleanup_vebox_ring:
  3874. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3875. cleanup_blt_ring:
  3876. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3877. cleanup_bsd_ring:
  3878. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3879. cleanup_render_ring:
  3880. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3881. return ret;
  3882. }
  3883. int
  3884. i915_gem_init_hw(struct drm_device *dev)
  3885. {
  3886. struct drm_i915_private *dev_priv = dev->dev_private;
  3887. int ret, i;
  3888. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3889. return -EIO;
  3890. if (dev_priv->ellc_size)
  3891. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3892. if (IS_HASWELL(dev))
  3893. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3894. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3895. if (HAS_PCH_NOP(dev)) {
  3896. if (IS_IVYBRIDGE(dev)) {
  3897. u32 temp = I915_READ(GEN7_MSG_CTL);
  3898. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3899. I915_WRITE(GEN7_MSG_CTL, temp);
  3900. } else if (INTEL_INFO(dev)->gen >= 7) {
  3901. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3902. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3903. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3904. }
  3905. }
  3906. i915_gem_init_swizzling(dev);
  3907. ret = dev_priv->gt.init_rings(dev);
  3908. if (ret)
  3909. return ret;
  3910. for (i = 0; i < NUM_L3_SLICES(dev); i++)
  3911. i915_gem_l3_remap(&dev_priv->ring[RCS], i);
  3912. /*
  3913. * XXX: Contexts should only be initialized once. Doing a switch to the
  3914. * default context switch however is something we'd like to do after
  3915. * reset or thaw (the latter may not actually be necessary for HW, but
  3916. * goes with our code better). Context switching requires rings (for
  3917. * the do_switch), but before enabling PPGTT. So don't move this.
  3918. */
  3919. ret = i915_gem_context_enable(dev_priv);
  3920. if (ret && ret != -EIO) {
  3921. DRM_ERROR("Context enable failed %d\n", ret);
  3922. i915_gem_cleanup_ringbuffer(dev);
  3923. return ret;
  3924. }
  3925. ret = i915_ppgtt_init_hw(dev);
  3926. if (ret && ret != -EIO) {
  3927. DRM_ERROR("PPGTT enable failed %d\n", ret);
  3928. i915_gem_cleanup_ringbuffer(dev);
  3929. }
  3930. return ret;
  3931. }
  3932. int i915_gem_init(struct drm_device *dev)
  3933. {
  3934. struct drm_i915_private *dev_priv = dev->dev_private;
  3935. int ret;
  3936. i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  3937. i915.enable_execlists);
  3938. mutex_lock(&dev->struct_mutex);
  3939. if (IS_VALLEYVIEW(dev)) {
  3940. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3941. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  3942. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  3943. VLV_GTLC_ALLOWWAKEACK), 10))
  3944. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3945. }
  3946. if (!i915.enable_execlists) {
  3947. dev_priv->gt.do_execbuf = i915_gem_ringbuffer_submission;
  3948. dev_priv->gt.init_rings = i915_gem_init_rings;
  3949. dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  3950. dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  3951. } else {
  3952. dev_priv->gt.do_execbuf = intel_execlists_submission;
  3953. dev_priv->gt.init_rings = intel_logical_rings_init;
  3954. dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  3955. dev_priv->gt.stop_ring = intel_logical_ring_stop;
  3956. }
  3957. ret = i915_gem_init_userptr(dev);
  3958. if (ret) {
  3959. mutex_unlock(&dev->struct_mutex);
  3960. return ret;
  3961. }
  3962. i915_gem_init_global_gtt(dev);
  3963. ret = i915_gem_context_init(dev);
  3964. if (ret) {
  3965. mutex_unlock(&dev->struct_mutex);
  3966. return ret;
  3967. }
  3968. ret = i915_gem_init_hw(dev);
  3969. if (ret == -EIO) {
  3970. /* Allow ring initialisation to fail by marking the GPU as
  3971. * wedged. But we only want to do this where the GPU is angry,
  3972. * for all other failure, such as an allocation failure, bail.
  3973. */
  3974. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3975. atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  3976. ret = 0;
  3977. }
  3978. mutex_unlock(&dev->struct_mutex);
  3979. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3980. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3981. dev_priv->dri1.allow_batchbuffer = 1;
  3982. return ret;
  3983. }
  3984. void
  3985. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3986. {
  3987. struct drm_i915_private *dev_priv = dev->dev_private;
  3988. struct intel_engine_cs *ring;
  3989. int i;
  3990. for_each_ring(ring, dev_priv, i)
  3991. dev_priv->gt.cleanup_ring(ring);
  3992. }
  3993. int
  3994. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3995. struct drm_file *file_priv)
  3996. {
  3997. struct drm_i915_private *dev_priv = dev->dev_private;
  3998. int ret;
  3999. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4000. return 0;
  4001. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  4002. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  4003. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  4004. }
  4005. mutex_lock(&dev->struct_mutex);
  4006. dev_priv->ums.mm_suspended = 0;
  4007. ret = i915_gem_init_hw(dev);
  4008. if (ret != 0) {
  4009. mutex_unlock(&dev->struct_mutex);
  4010. return ret;
  4011. }
  4012. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  4013. ret = drm_irq_install(dev, dev->pdev->irq);
  4014. if (ret)
  4015. goto cleanup_ringbuffer;
  4016. mutex_unlock(&dev->struct_mutex);
  4017. return 0;
  4018. cleanup_ringbuffer:
  4019. i915_gem_cleanup_ringbuffer(dev);
  4020. dev_priv->ums.mm_suspended = 1;
  4021. mutex_unlock(&dev->struct_mutex);
  4022. return ret;
  4023. }
  4024. int
  4025. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  4026. struct drm_file *file_priv)
  4027. {
  4028. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4029. return 0;
  4030. mutex_lock(&dev->struct_mutex);
  4031. drm_irq_uninstall(dev);
  4032. mutex_unlock(&dev->struct_mutex);
  4033. return i915_gem_suspend(dev);
  4034. }
  4035. void
  4036. i915_gem_lastclose(struct drm_device *dev)
  4037. {
  4038. int ret;
  4039. if (drm_core_check_feature(dev, DRIVER_MODESET))
  4040. return;
  4041. ret = i915_gem_suspend(dev);
  4042. if (ret)
  4043. DRM_ERROR("failed to idle hardware: %d\n", ret);
  4044. }
  4045. static void
  4046. init_ring_lists(struct intel_engine_cs *ring)
  4047. {
  4048. INIT_LIST_HEAD(&ring->active_list);
  4049. INIT_LIST_HEAD(&ring->request_list);
  4050. }
  4051. void i915_init_vm(struct drm_i915_private *dev_priv,
  4052. struct i915_address_space *vm)
  4053. {
  4054. if (!i915_is_ggtt(vm))
  4055. drm_mm_init(&vm->mm, vm->start, vm->total);
  4056. vm->dev = dev_priv->dev;
  4057. INIT_LIST_HEAD(&vm->active_list);
  4058. INIT_LIST_HEAD(&vm->inactive_list);
  4059. INIT_LIST_HEAD(&vm->global_link);
  4060. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  4061. }
  4062. void
  4063. i915_gem_load(struct drm_device *dev)
  4064. {
  4065. struct drm_i915_private *dev_priv = dev->dev_private;
  4066. int i;
  4067. dev_priv->slab =
  4068. kmem_cache_create("i915_gem_object",
  4069. sizeof(struct drm_i915_gem_object), 0,
  4070. SLAB_HWCACHE_ALIGN,
  4071. NULL);
  4072. INIT_LIST_HEAD(&dev_priv->vm_list);
  4073. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  4074. INIT_LIST_HEAD(&dev_priv->context_list);
  4075. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4076. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4077. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4078. for (i = 0; i < I915_NUM_RINGS; i++)
  4079. init_ring_lists(&dev_priv->ring[i]);
  4080. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4081. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4082. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4083. i915_gem_retire_work_handler);
  4084. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4085. i915_gem_idle_work_handler);
  4086. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4087. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4088. if (!drm_core_check_feature(dev, DRIVER_MODESET) && IS_GEN3(dev)) {
  4089. I915_WRITE(MI_ARB_STATE,
  4090. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  4091. }
  4092. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4093. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4094. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4095. dev_priv->fence_reg_start = 3;
  4096. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4097. dev_priv->num_fence_regs = 32;
  4098. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4099. dev_priv->num_fence_regs = 16;
  4100. else
  4101. dev_priv->num_fence_regs = 8;
  4102. /* Initialize fence registers to zero */
  4103. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4104. i915_gem_restore_fences(dev);
  4105. i915_gem_detect_bit_6_swizzle(dev);
  4106. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4107. dev_priv->mm.interruptible = true;
  4108. dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  4109. dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
  4110. dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
  4111. register_shrinker(&dev_priv->mm.shrinker);
  4112. dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  4113. register_oom_notifier(&dev_priv->mm.oom_notifier);
  4114. mutex_init(&dev_priv->fb_tracking.lock);
  4115. }
  4116. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4117. {
  4118. struct drm_i915_file_private *file_priv = file->driver_priv;
  4119. cancel_delayed_work_sync(&file_priv->mm.idle_work);
  4120. /* Clean up our request list when the client is going away, so that
  4121. * later retire_requests won't dereference our soon-to-be-gone
  4122. * file_priv.
  4123. */
  4124. spin_lock(&file_priv->mm.lock);
  4125. while (!list_empty(&file_priv->mm.request_list)) {
  4126. struct drm_i915_gem_request *request;
  4127. request = list_first_entry(&file_priv->mm.request_list,
  4128. struct drm_i915_gem_request,
  4129. client_list);
  4130. list_del(&request->client_list);
  4131. request->file_priv = NULL;
  4132. }
  4133. spin_unlock(&file_priv->mm.lock);
  4134. }
  4135. static void
  4136. i915_gem_file_idle_work_handler(struct work_struct *work)
  4137. {
  4138. struct drm_i915_file_private *file_priv =
  4139. container_of(work, typeof(*file_priv), mm.idle_work.work);
  4140. atomic_set(&file_priv->rps_wait_boost, false);
  4141. }
  4142. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4143. {
  4144. struct drm_i915_file_private *file_priv;
  4145. int ret;
  4146. DRM_DEBUG_DRIVER("\n");
  4147. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4148. if (!file_priv)
  4149. return -ENOMEM;
  4150. file->driver_priv = file_priv;
  4151. file_priv->dev_priv = dev->dev_private;
  4152. file_priv->file = file;
  4153. spin_lock_init(&file_priv->mm.lock);
  4154. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4155. INIT_DELAYED_WORK(&file_priv->mm.idle_work,
  4156. i915_gem_file_idle_work_handler);
  4157. ret = i915_gem_context_open(dev, file);
  4158. if (ret)
  4159. kfree(file_priv);
  4160. return ret;
  4161. }
  4162. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4163. struct drm_i915_gem_object *new,
  4164. unsigned frontbuffer_bits)
  4165. {
  4166. if (old) {
  4167. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4168. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4169. old->frontbuffer_bits &= ~frontbuffer_bits;
  4170. }
  4171. if (new) {
  4172. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4173. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4174. new->frontbuffer_bits |= frontbuffer_bits;
  4175. }
  4176. }
  4177. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  4178. {
  4179. if (!mutex_is_locked(mutex))
  4180. return false;
  4181. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  4182. return mutex->owner == task;
  4183. #else
  4184. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  4185. return false;
  4186. #endif
  4187. }
  4188. static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
  4189. {
  4190. if (!mutex_trylock(&dev->struct_mutex)) {
  4191. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  4192. return false;
  4193. if (to_i915(dev)->mm.shrinker_no_lock_stealing)
  4194. return false;
  4195. *unlock = false;
  4196. } else
  4197. *unlock = true;
  4198. return true;
  4199. }
  4200. static int num_vma_bound(struct drm_i915_gem_object *obj)
  4201. {
  4202. struct i915_vma *vma;
  4203. int count = 0;
  4204. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4205. if (drm_mm_node_allocated(&vma->node))
  4206. count++;
  4207. return count;
  4208. }
  4209. static unsigned long
  4210. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  4211. {
  4212. struct drm_i915_private *dev_priv =
  4213. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4214. struct drm_device *dev = dev_priv->dev;
  4215. struct drm_i915_gem_object *obj;
  4216. unsigned long count;
  4217. bool unlock;
  4218. if (!i915_gem_shrinker_lock(dev, &unlock))
  4219. return 0;
  4220. count = 0;
  4221. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  4222. if (obj->pages_pin_count == 0)
  4223. count += obj->base.size >> PAGE_SHIFT;
  4224. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4225. if (!i915_gem_obj_is_pinned(obj) &&
  4226. obj->pages_pin_count == num_vma_bound(obj))
  4227. count += obj->base.size >> PAGE_SHIFT;
  4228. }
  4229. if (unlock)
  4230. mutex_unlock(&dev->struct_mutex);
  4231. return count;
  4232. }
  4233. /* All the new VM stuff */
  4234. unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4235. struct i915_address_space *vm)
  4236. {
  4237. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4238. struct i915_vma *vma;
  4239. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4240. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4241. if (vma->vm == vm)
  4242. return vma->node.start;
  4243. }
  4244. WARN(1, "%s vma for this object not found.\n",
  4245. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4246. return -1;
  4247. }
  4248. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4249. struct i915_address_space *vm)
  4250. {
  4251. struct i915_vma *vma;
  4252. list_for_each_entry(vma, &o->vma_list, vma_link)
  4253. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4254. return true;
  4255. return false;
  4256. }
  4257. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4258. {
  4259. struct i915_vma *vma;
  4260. list_for_each_entry(vma, &o->vma_list, vma_link)
  4261. if (drm_mm_node_allocated(&vma->node))
  4262. return true;
  4263. return false;
  4264. }
  4265. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4266. struct i915_address_space *vm)
  4267. {
  4268. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4269. struct i915_vma *vma;
  4270. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4271. BUG_ON(list_empty(&o->vma_list));
  4272. list_for_each_entry(vma, &o->vma_list, vma_link)
  4273. if (vma->vm == vm)
  4274. return vma->node.size;
  4275. return 0;
  4276. }
  4277. static unsigned long
  4278. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  4279. {
  4280. struct drm_i915_private *dev_priv =
  4281. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4282. struct drm_device *dev = dev_priv->dev;
  4283. unsigned long freed;
  4284. bool unlock;
  4285. if (!i915_gem_shrinker_lock(dev, &unlock))
  4286. return SHRINK_STOP;
  4287. freed = i915_gem_purge(dev_priv, sc->nr_to_scan);
  4288. if (freed < sc->nr_to_scan)
  4289. freed += __i915_gem_shrink(dev_priv,
  4290. sc->nr_to_scan - freed,
  4291. false);
  4292. if (unlock)
  4293. mutex_unlock(&dev->struct_mutex);
  4294. return freed;
  4295. }
  4296. static int
  4297. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  4298. {
  4299. struct drm_i915_private *dev_priv =
  4300. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  4301. struct drm_device *dev = dev_priv->dev;
  4302. struct drm_i915_gem_object *obj;
  4303. unsigned long timeout = msecs_to_jiffies(5000) + 1;
  4304. unsigned long pinned, bound, unbound, freed;
  4305. bool was_interruptible;
  4306. bool unlock;
  4307. while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
  4308. schedule_timeout_killable(1);
  4309. if (fatal_signal_pending(current))
  4310. return NOTIFY_DONE;
  4311. }
  4312. if (timeout == 0) {
  4313. pr_err("Unable to purge GPU memory due lock contention.\n");
  4314. return NOTIFY_DONE;
  4315. }
  4316. was_interruptible = dev_priv->mm.interruptible;
  4317. dev_priv->mm.interruptible = false;
  4318. freed = i915_gem_shrink_all(dev_priv);
  4319. dev_priv->mm.interruptible = was_interruptible;
  4320. /* Because we may be allocating inside our own driver, we cannot
  4321. * assert that there are no objects with pinned pages that are not
  4322. * being pointed to by hardware.
  4323. */
  4324. unbound = bound = pinned = 0;
  4325. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4326. if (!obj->base.filp) /* not backed by a freeable object */
  4327. continue;
  4328. if (obj->pages_pin_count)
  4329. pinned += obj->base.size;
  4330. else
  4331. unbound += obj->base.size;
  4332. }
  4333. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4334. if (!obj->base.filp)
  4335. continue;
  4336. if (obj->pages_pin_count)
  4337. pinned += obj->base.size;
  4338. else
  4339. bound += obj->base.size;
  4340. }
  4341. if (unlock)
  4342. mutex_unlock(&dev->struct_mutex);
  4343. pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
  4344. freed, pinned);
  4345. if (unbound || bound)
  4346. pr_err("%lu and %lu bytes still available in the "
  4347. "bound and unbound GPU page lists.\n",
  4348. bound, unbound);
  4349. *(unsigned long *)ptr += freed;
  4350. return NOTIFY_DONE;
  4351. }
  4352. struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
  4353. {
  4354. struct i915_vma *vma;
  4355. vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link);
  4356. if (vma->vm != i915_obj_to_ggtt(obj))
  4357. return NULL;
  4358. return vma;
  4359. }